Is it possible to use jQuery .on and hover?

前端 未结 10 777
失恋的感觉
失恋的感觉 2020-11-22 11:03

I have a

    that is populated with javascript after the initial page load. I\'m currently using .bind with mouseover and
10条回答
  •  囚心锁ツ
    2020-11-22 11:43

    Just surfed in from the web and felt I could contribute. I noticed that with the above code posted by @calethebrewer can result in multiple calls over the selector and unexpected behaviour for example: -

    $(document).on('mouseover', '.selector', function() {
       //do something
    });
    $(document).on('mouseout', '.selector', function() {
       //do something
    });
    

    This fiddle http://jsfiddle.net/TWskH/12/ illustraits my point. When animating elements such as in plugins I have found that these multiple triggers result in unintended behavior which may result in the animation or code being called more than is necessary.

    My suggestion is to simply replace with mouseenter/mouseleave: -

    $(document).on('mouseenter', '.selector', function() {
       //do something
    });
    $(document).on('mouseleave', '.selector', function() {
       //do something
    });
    

    Although this prevented multiple instances of my animation from being called, I eventually went with mouseover/mouseleave as I needed to determine when children of the parent were being hovered over.

提交回复
热议问题