JavaScript and JQuery mouse event bubble Processing

Source: Internet
Author: User

JavaScript and JQuery mouse event bubble Processing

This article mainly introduces the mouse and mouse event bubbling processing of JavaScript and JQuery. This article summarizes some conclusions about the mouse event and provides the JavaScript and JQuery Test Code respectively, for more information, see

Simple mouse movement event:

Enter

The Code is as follows:

Mouseenter: Do not bubble

Mouseover: Bubble

The mouseover event is triggered no matter the mouse pointer passes through the selected element or its child element.

The mouseenter event is triggered only when the mouse pointer passes through the selected element.

Remove

The Code is as follows:

Mouseleave: Do not bubble

Mouseout: Bubble

The mouseout event is triggered no matter whether the mouse pointer leaves the selected element or any child element.

The mouseleave event is triggered only when the mouse pointer leaves the selected element.

Let's look at the problem through a case:

Bind a nested layer to a mouseout event, and you will find that the mouseout event is different from what you think.

The Code is as follows:

  

External child element

Internal child element

0

 

  

0

 

  

 

We found a problematic mouseout event:

1. Bubble cannot be blocked

2. It will also be triggered on the internal child element

There is also a mouseover event, so how do we stop the bubble when the stopPropagation method fails?

1. To prevent repeated triggering of mouseover and mouseout, A relatedTarget attribute of the event object is used to determine the attributes of the relevant nodes of the mouseover and mouseout event Target nodes. Simply put, when the mouseover event is triggered, the relatedTarget attribute represents the node where the mouse has just left. When the mouseout event is triggered, it represents the object where the mouse moves. MSIE does not support this attribute, but it has a substitute attribute, namely fromElement and toElement.

2. With this attribute, we can clearly know the object from which the mouse is moved and where it is to be moved. In this way, we can determine whether the associated object is inside the object where the event is to be triggered, or whether it is the object itself. Through this judgment, we can reasonably choose whether the event is to be triggered.

3. Here we also use the contains method to check whether an object is contained in another object. MSIE and FireFox provide the checking methods respectively. A function is encapsulated here.

JQuery's processing is exactly the same.

Copy the Code as follows:

JQuery. each ({

Mouseenter: "mouseover ",

Mouseleave: "mouseout ",

Pointerenter: "pointerover ",

Pointerleave: "pointerout"

}, Function (orig, fix ){

JQuery. event. special [orig] = {

DelegateType: fix,

BindType: fix,

Handle: function (event ){

Var ret,

Target = this,

Related = event. relatedTarget,

HandleObj = event. handleObj;

// For mousenter/leave call the handler if related is outside the target.

// NB: No relatedTarget if the mouse left/entered the browser window

If (! Related | (related! = Target &&! JQuery. contains (target, related ))){

Event. type = handleObj. origType;

Ret = handleObj. handler. apply (this, arguments );

Event. type = fix;

}

Return ret;

}

};

});

Related Article

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.