How do I ignore mouse events on child elements in jQuery?

前端 未结 5 1203
独厮守ぢ
独厮守ぢ 2020-12-08 10:38

I have an unordered list with mouseover and mouseout events attached to the li elements. Each contains a link and there is a bit of padding in the li. When I mouseover the l

5条回答
  •  余生分开走
    2020-12-08 11:13

    Use "event.stopPropagation()" to stop the events from bubbling upwards:

      jQuery('#menu li a').mouseover( function(evt) {
           evt.stopPropagation();
           return false;
      });
      jQuery('#menu li a').mouseout( function(evt) {
           evt.stopPropagation();
           return false;
      });
    

    Alternatively use the mouseenter and mouseleave events instead of mouseover/out. jQuery normalizes their behaviour across browsers and these events have the benefit of not bubbling...

提交回复
热议问题