jQuery .click 不断添加事件,而不是替换它
我有 以下代码块:
function testJQueryClick(){
$('#button2').click(function(){ alert ('Debug'); return false; });
}
当我调用该函数一次(然后单击按钮 2)时,我会收到预期的警报。当我第二次调用相同的函数然后单击按钮 2 时,我收到两个警报,第三次收到三个警报,等等。jQuery 似乎每次都会附加 .click
事件,而不是替换它。
我希望每次调用 .click
处理程序时都会替换它。我在 jQuery 文档 中找不到任何内容来确认这是预期的行为,或者不是。
I have the following code block:
function testJQueryClick(){
$('#button2').click(function(){ alert ('Debug'); return false; });
}
When I call the function once (and then click on button 2), I get the expected alert. When I call the same function a second time and then click button 2, I get two alerts, third time I get three, etc. jQuery seems to be appending the .click
event each time, rather than replacing it.
I would expect that the .click
handler to be replaced each time I call it. I can't find anything in the jQuery documentation that either confirms this as the expected behaviour, or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要首先调用
off
来摆脱旧的事件侦听器:在笔中查看实际操作。
You'll want to first call
off
the get rid of the old event listener:See it in the Pen in action.
请查看 http://api.jquery.com/bind/。事件绑定方法,例如“click()”、“focus()”等...只是bind(eventtype)的快捷方式,因此阅读bind()文档将让您深入了解如何所有事件绑定都有效。我认为与您的具体问题相关的一点是:
“当事件到达某个元素时,绑定到该元素的该事件类型的所有处理程序都会被触发。”
这意味着如果您将处理程序“function(){alert('Debug'); return false; }”绑定到点击事件 5 次,则所有 5 次都会执行。
作为旁注,绑定到 onclick 处理程序的首选且更现代的方法是完全绕过链接的“onclick”属性,并使用 jQuery 在 DOM 加载上执行所有绑定。这是一个使用更现代的绑定的示例,并且还利用 trigger() 方法在单击 #button1 时触发 #button2 单击处理程序(我认为这就是您在示例中试图实现的目标):
Check out the documentation for the bind() method at http://api.jquery.com/bind/. The event binding method's such as "click()", "focus()", etc... are just shortcuts for bind(eventtype) so reading the bind() docs will give you insight into how all of the event binds work. I think the bit that pertains to your specific issue is:
"When an event reaches an element, all handlers bound to that event type for the element are fired."
Meaning if you bind the handler "function(){ alert ('Debug'); return false; }" to the click event 5 times, all 5 will execute.
As a sidenote, the preferred and more modern way to bind to an onclick handler is to completely bypass the "onclick" attribute of a link and perform all your binds on DOM load using jQuery. Here's an example that uses more modern binds and also utilizes the trigger() method to fire the #button2 click handler when #button1 is clicked (which is what I think you were trying to achieve in your example):
绑定事件 jquery 不会考虑控件上已绑定的事件,它只会绑定另一个事件。
取消绑定例如:
您可以使用
unbind
API 首先删除绑定事件,或者如果它是相同的点击事件处理程序,但由于具有相同选择器的新控件已插入到 dom 中,因此您会多次调用它,那么您可能应该考虑使用live
api..希望有帮助..
while binding an event jquery does not consider what events are already bound on the control it just binds one more event..
unbind eg:
you could use the
unbind
API to remove the bound event first or if its the same click event handler but you are calling it multiple times since a new control with the same selector has been inserted to the dom then you should probably look at using thelive
api..Hope tihs helps..
如前所述,如果您想替换旧的点击处理程序,则需要先删除旧的点击处理程序,然后再添加新的点击处理程序。
从 jQuery 1.7 开始,首选方法是使用 off。 (
unbind
从 jQuery 3 开始已被弃用)例如:
As previously noted you need to remove old click handlers before you add the new one if you want to replace it.
As of jQuery 1.7 the preferred way is to use off. (
unbind
is deprecated as of jQuery 3)For example:
这是因为每次您分配一个新的点击处理程序。应该是这样的:
当然是在文档加载之后。
It's because every time you are assigning a new click handler. It should just be this:
After the document loads of course.