JavaScript block event bubbling sample sharing _javascript Tips

Source: Internet
Author: User

Before the incident bubbling problem, also went to the internet to search for some, the results are mostly the same code, posted to the time is not very smooth. When it comes to FF, you can use E.stoppropagation (); , somehow I didn't succeed. However, I find that FF supports e.cancelbubble = True, and is tested and feasible. Just post the code here, and the province can look everywhere. The compatibility of previous versions of IE has not been tested, and then perfected it.

Copy Code code as follows:

Cancel Event Bubbling
function Stopbubble (e) {
var evt = (e)? e:window.event;//compatible FF
Evt.cancelbubble = true;  Evt.stoppropagation (); FF block Bubble, said to be able to use
};

Other:

1, cancelbubble (HTML DOM Event object property): If the event handle wants to prevent an event from propagating to an containment object, you must set the property to True.

2, Stoppropagation (HTML DOM Event object method): Termination of events in the propagation process of capture, target processing or foaming phase further spread. When this method is invoked, the handler for the event on the node is invoked and the event is no longer assigned to another node.

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 blocking event bubbling function you want to prevent the event from bubbling.

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.