JavaScript / jQuery: event fired twice

I am trying to bind events with jQuery and I have this simple code in my document:

$(document).ready(function() {
    $("p a").each(function(i, selected){
        $(selected).hover(
            function(event) { alert('over: ' + event.type); },
            function(event) { alert('out: ' + event.type); });
        }
    )
});

So, when I move the mouse over the link located inside the paragraph (the "p a" selector), a pop-up window appears twice. So, mouseenter and mouseleave run twice.

This only happens on Google Chrome. In Internet Explorer 7, Firefox 3, Opera, and Safari, both events are fired only once.

Why is this happening?

+2
source share
2 answers

, - Chrome. .

$(document).ready(function() {
  $("p a").each(function(i, selected){
    $(selected).hover( 
      function(event) { $("p").append('<br>over: ' + event.type); }, 
      function(event) { $("p").append(' out: ' + event.type); });
    }
  )
});
+6

, , , . , , javascript .

, , . , !

+8

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


All Articles