Event bubbling and default event Behavior

Source: Internet
Author: User

1. Original Ecological events

Get Original event objects:

<button onclick="get(event)" type="button">tijiao</button>

Prevent event bubbles:

1 // if the event object is provided, this is a non-IE browser 2 if (E & E. stoppropagation) 3 // Therefore, it supports the W3C stoppropagation () method 4 E. stoppropagation (); 5 else6 // otherwise, we need to use IE to cancel event bubbling 7 E. cancelbubble = true;

Prevent default behavior:

1 // if the event object is provided, this is a non-IE browser 2 if (E & E. preventdefault) {3 // block the default browser action (W3C) 4 E. preventdefault (); 5 6} else {7 // method of blocking the default action of the function in IE 8 E. returnvalue = false; 9}

Event target object:

1 var target = NULL; 2 if (E & E. srcelement) {3 // supported by IE. ie10 also contains the attribute target. 4 target = E. srcelement; 5} else {6 target = e.tar get; 7}

 

 

2. jquery events

Jquery encapsulates events. You need to distinguish between original Event Events and jquery events. Jquery events can directly call the jquery API method.

Prevent default behavior:

 $("a").click(function(event) {               event.preventDefault();               $(‘<div/>‘).append(‘default ‘ + event.type + ‘ prevented‘).appendTo(‘#log‘);             });

Prevent event bubbles:

1 $("p").click(function(event){2     event.stopPropagation();    // do something3   });  

 

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.