Pass variables to jQuery function
so simple question! How can I 开发者_StackOverflow中文版pass a variable into a jQuery function, like in Javascript?
<script type="text/javascript">
function foo(bar) {
$("#content").load(bar);
}
</script>
<a href="#" onClick="foo('test.php');">Load</a>
The above code doesn't work, clearly, but hopefully you can get an idea for what I'm asking.
You are programming in JavaScript.
jQuery is just a library - a bunch of JavaScript functions you can use; it is not another language.
That said, a more modern way of achieving your goal is to use jQuery for event binding
<a href="#" id="LoadLink">Load</a>
.
function foo(bar) {
$("#content").load(bar);
}
$(document).ready(function(){
$("#LoadLink").click(function(){
foo("test.php");
return false; //link won't scroll to the top
}); //click
});//ready
Working example: http://jsfiddle.net/wtL88/
<a href="javascript:;" onclick="javascript:foo('test.php');">Load</a>
精彩评论