javascript Onclick 事件被自动调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10101899/
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
Onclick event getting called automatically
提问by Sriram
I wrote a JavaScript class called MyClass
in which I've defined a method closeThis
我写了一个 JavaScript 类,MyClass
在其中定义了一个方法closeThis
MyClass = function() {
this.closeThis = function() {
document.getElementById("hidePane").style.display = 'none';
}
}
Now, in my html, i'm trying to call that as follows...
现在,在我的 html 中,我试图按如下方式调用它...
<script type="text/javascript">
function callThis() {
var myclassObj = new MyClass();
document.getElementById("closeButton").onclick = myclassObj.closeThis();
}
</script>
The above callThis
will be called when I clicked on a button. The problem here is, onclick
event on top of clsoeButtion
is getting called automatically when page loads. What could be wrong in this?
callThis
当我点击一个按钮时,上面将被调用。这里的问题是,页面加载时会自动调用onclick
顶部的事件clsoeButtion
。这可能有什么问题?
回答by jmort253
You're calling the function right away.
您正在立即调用该函数。
When you leave the parentheses on the function reference, what you're basically saying is:
当您在函数引用上留下括号时,您基本上是在说:
Evaluate the closeThis function and assign the result to onclick
评估 closeThis 函数并将结果分配给 onclick
when what you really want to do is assign the function reference to the click handler:
当您真正想要做的是将函数引用分配给点击处理程序时:
document.getElementById("closeButton").onclick = myclassObj.closeThis;
Leave out the parentheses instead, and you'll bind the closeThis function to the onclick. What this instead says is:
相反,省略括号,您会将 closeThis 函数绑定到 onclick。这反而说的是:
Assign the function closeThis to the click handler.
将函数 closeThis 分配给点击处理程序。
You are essentially assigning the function to the variable as a first-class object, or a reference to a function.
您实际上是将函数作为第一类对象或对函数的引用分配给变量。
As an aside, my personal preference is to always use an anonymous function wrapper. Sometimes you need to be able to pass parameters into your function, and this makes sure that you can more easily do so:
顺便说一句,我个人的偏好是始终使用匿名函数包装器。有时您需要能够将参数传递到您的函数中,这确保您可以更轻松地这样做:
document.getElementById("closeButton").onclick =
function() {
myclassObj.closeThis();
};
回答by Trinh Hoang Nhu
it should be
document.getElementById("closeButton").onclick = myclassObj.closeThis;
not myclassObj.closeThis();
它应该是
document.getElementById("closeButton").onclick = myclassObj.closeThis;
不 myclassObj.closeThis();
myclassObj.closeThis()
will call the function then assign value to onclick
myclassObj.closeThis()
将调用该函数然后将值分配给 onclick
回答by Sarfraz
You need to remove ()
from it otherwise it gets called immediately because that's how you call a function by suffixing ()
, so simply remove these braces:
您需要从中删除()
它,否则它会立即被调用,因为这就是您通过后缀调用函数的方式()
,因此只需删除这些大括号:
document.getElementById("closeButton").onclick = myclassObj.closeThis;