最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery bind firing multiple times? - Stack Overflow

programmeradmin5浏览0评论
$(".container").on("contextmenu", ".photos-bottom .albums li", function(e) {

$('html').bind('click', function (event) {
    alert(id);
});

return false;
});

when I right click (for the contextmenu) multiple times and then left click html once, it triggers the alert the number of times that I right clicked.

So if I right click once, then left click, it shows a popup once. If I right click three times, then left click, it shows the popup three times.

Why is this so?

$(".container").on("contextmenu", ".photos-bottom .albums li", function(e) {

$('html').bind('click', function (event) {
    alert(id);
});

return false;
});

when I right click (for the contextmenu) multiple times and then left click html once, it triggers the alert the number of times that I right clicked.

So if I right click once, then left click, it shows a popup once. If I right click three times, then left click, it shows the popup three times.

Why is this so?

Share Improve this question asked Jan 28, 2012 at 18:22 Dylan CrossDylan Cross 5,98623 gold badges79 silver badges120 bronze badges 3
  • what are you trying to do with that code? attaching click event to html? – gdoron Commented Jan 28, 2012 at 18:29
  • Why do you re-bind the click handler in the contextmenu handler? This does not make a lot of sense. – Tomalak Commented Jan 28, 2012 at 18:29
  • This is a popup, and when I bind click to html it's so when i click outside of the popup it fires this event to close the menu, and whatever else. (There's a lot more code to this, so there's a lot you don't see) – Dylan Cross Commented Jan 28, 2012 at 18:31
Add a comment  | 

2 Answers 2

Reset to default 19

$('html').unbind('click').bind('click') fixed it.

Because your click event is being bound every time a context menu event occurs, you're actually adding an additional bind each time you right click. This is the reason for the ever-growing number of event executions.

You should either:

a) unbind the event when the context menu is closed, or

b) bind the click event outside of your contextmenu callback function.

发布评论

评论列表(0)

  1. 暂无评论