Example of JavaScript blocking event bubbling, javascript example

Source: Internet
Author: User

Example of JavaScript blocking event bubbling, javascript example

Previously, I encountered an event bubble problem and went to the Internet to search for it. Most of the results were the same code, and the post was not very smooth. When FF is involved, you can use e. stopPropagation ();. Somehow I did not succeed. However, I found that FF supports e. cancelBubble = true;, which is feasible after testing. Paste the code here, so you can find it everywhere. The compatibility of earlier versions of IE has not been tested yet. It can be improved later.

 Copy codeThe Code is as follows:
// Cancel event bubbling
Function stopBubble (e ){
Var evt = (e )? E: window. event; // compatible with FF
Evt. cancelBubble = true; // evt. stopPropagation (); FF can be used to prevent bubbles.
};

In addition:

1. cancelBubble (html dom Event object attribute): If the Event handle wants to prevent the Event from spreading to an inclusive object, it must be set to true.

2. stopPropagation (html dom Event object method): terminates further propagation of an Event in the capture, target processing, or blister phase of the propagation process. After this method is called, the handler for processing the event on the node will be called, and the event will not be assigned to other nodes.

3. preventDefault (html dom Event object method) notifies the browser not to perform the default action associated with the Event.

Example:
Function stopBubble (e)
{
If (e & e. stopPropagation)
E. stopPropagation ()
Else
Window. event. cancelBubble = true
}

Put this stopBubble (e) function in the function that you want to stop event bubbles to prevent event bubbles.

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.