A solution to the problem of nested mouseenter,mouseenter functions executing multiple times in jquery hover

Source: Internet
Author: User

It is known that the API hover in JQ is the combination of MouseEnter and MouseLeave, which is implemented in JQ ' Hover:function (fnover,fnout) {
Return This.mouseenter (Fnover). MouseLeave (fnout| | Fnover)
}。

The code is as follows

<! DOCTYPE html>

The problem is that after the code executes. When the mouse moves, the code executes.

With what we want, when the mouse enters, the internal not performing effect is inconsistent.

Each time MouseEnter, will add the binding event to DD, the binding event will not be overwritten, but will be executed according to the binding sequence, so it will be executed many times, event.stopPropagation is to prevent bubbling, does not block other events on the same node.

Master one can put the binding event to DD independent of hover, and second, can be hover after the end of the DD. Take a look at the following code:

Bind individually

$ (function () {    $ ('. Member-list dd '). On (' MouseEnter ', function () {        console.log (this);    });    $ ('. Unit-list '). Hover (function () {        console.log (' e n T e R ');    },function () {        console.log (' L e a v e ');    });});

hoverPost-undo Binding

<script>$ (function () {    var fmouseenter = function (e) {        console.log (this);    };    var jDd = $ ('. Member-list dd ');    $ ('. Unit-list '). Hover (function (e) {        jdd.on (' MouseEnter ', fmouseenter);        Console.log (' e n T e R ');    },function () {        jdd.off (' MouseEnter ', fmouseenter);        Console.log (' L e a v e ');})    ; </script>

Original address: https://yq.aliyun.com/ask/18480

A solution to the problem of nested mouseenter,mouseenter functions executing multiple times in jquery hover

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.