admin管理员组文章数量:1398195
I feel so so stupid for forgetting this, but I've been out of practice for a minute, and I'm drawing a blank.
Why is slideDown being called onload rather than when the click is handled?
function buttonClicked(buttonNumber) {
$contentBox.slideDown("slow");
};
$button1.click = buttonClicked(1);
I feel so so stupid for forgetting this, but I've been out of practice for a minute, and I'm drawing a blank.
Why is slideDown being called onload rather than when the click is handled?
function buttonClicked(buttonNumber) {
$contentBox.slideDown("slow");
};
$button1.click = buttonClicked(1);
Share
Improve this question
asked Jun 12, 2013 at 16:27
Tom EllisTom Ellis
852 silver badges7 bronze badges
1
-
4
Because you are calling
buttonClicked
. The()
after a function reference always calls the function. Example:function foo() { alert(42); }; foo();
. Herefoo
is called because I put()
after the variable name. Btw, if$button1
is a jQuery object, then you have to pass a function reference to the.click
method, not assign a value to it. See learn.jquery./events. – Felix Kling Commented Jun 12, 2013 at 16:28
2 Answers
Reset to default 9You would want to structure it as
$button1.click(function() {
buttonClicked(1);
});
This will make it fire when $button1
is clicked.
Try this:
$button1.click(function() {
buttonClicked(1);
});
See documentation at api.jquery.
本文标签: jqueryJavaScriptDefine function without calling itStack Overflow
版权声明:本文标题:jquery - JavaScript - Define function without calling it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744174977a2593955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论