JQuery link shooting multiple times?

$(".container").on("contextmenu", ".photos-bottom .albums li", function(e) { $('html').bind('click', function (event) { alert(id); }); return false; }); 

when I right-clicked (for the context menu) several times, and then clicked html once, it triggers a warning the number of times I right-clicked.

So, if I right-clicked once, then left-clicked, it will pop up once. If I right-click three times, then left-click, it will pop up three times.

Why is this so?

+6
source share
2 answers

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

+19
source

Since your click event is attached at every event in the context menu, you actually add extra binding every time you right-click. This is the reason for the ever-increasing number of events.

You must either:

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

b) bind a click event outside the contextmenu callback function.

+4
source

Source: https://habr.com/ru/post/907118/


All Articles