使用 click() 在 jQuery 中调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/447414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a function in jQuery with click()
提问by Edward Tanguay
In the code below, why does the openfunction work but the closefunction does not?
在下面的代码中,为什么open函数有效而close函数无效?
$("#closeLink").click("closeIt");
How do you just calla function in click()
instead of definingit in the click()
method?
你如何只调用一个函数click()
而不是在方法中定义它click()
?
<script type="text/javascript">
$(document).ready(function() {
$("#openLink").click(function() {
$("#message").slideDown("fast");
});
$("#closeLink").click("closeIt");
});
function closeIt() {
$("#message").slideUp("slow");
}
</script>
My HTML:
我的 HTML:
Click these links to <span id="openLink">open</span>
and <span id="closeLink">close</span> this message.</div>
<div id="message" style="display: none">This is a test message.</div>
回答by Tiago
$("#closeLink").click(closeIt);
Let's say you want to call your function passing some args to it i.e., closeIt(1, false)
. Then, you should build an anonymous function and call closeIt
from it.
假设您想调用您的函数并传递一些参数给它,即closeIt(1, false)
. 然后,您应该构建一个匿名函数并closeIt
从中调用。
$("#closeLink").click(function() {
closeIt(1, false);
});