Block event bubbling

Source: Internet
Author: User

< div id= "one" onclick= "alert (' I am outermost ');" >< div id= "onclick=" alert (' I am the middle tier '); >< a id= "Hr_three" href= "http://www.baidu.com" onclick= "alert (' I'm the innermost ');" > Click me < /a ></div ></div >

1.event.stoppropagation ();
Event bubbling is blocked during event processing, but the default behavior is not blocked (it performs a hyperlink jump)
2.return false;
Event bubbling is blocked during event processing, and the default behavior is also blocked (for example, a jump that did not perform a hyperlink just now)
There is also a bubbling-related:
3.event.preventdefault ();
If you put it in the Click event of the head a tag, click "Click Me".
Will find it pop up in turn: I am the innermost----> I am the middle tier----> I am the outermost, but finally did not jump to Baidu
Its role is: During event processing, do not halt the event bubbling, but the default behavior (it only executes all the bullets, but did not perform a hyperlink jump)

4.The difference between this and event.target:

The event in

JS is bubbling, so this is changeable, but Event.target does not change, it is always the target DOM element that directly accepts the event;
the same point of this and event.target:
This and Event.target are DOM objects, and if you want to use the methods in Jquey, you can convert them to jquery objects: $ (this) and $ (event.target);  

Compatibility function for 5.js event handling: var eventutil = {  addhandler:function (element, type, handler) {    if ( Element.addeventlistener) {      Element.addeventlistener (type, handler, false);   } else if ( element.attachevent) {      element.attachevent ("on" + type, handler);   } else {    &N Bsp Element["on" + type] = handler;   } },  removehandler:function (element, type, handler) {  &nbsp ; if (Element.removeeventlistener) {      Element.removeeventlistener (type, handler, false);    } else if (element.detachevent) {      element.detachevent ("on" + type, handler);   } else {&nbsp ;     element["on" + type] = null;   } },  getevent:function (event) {    return EV Ent? event:window.event; },  gettarget:function (event) {    return Event.target | | event.srcelement;& nbsp },  preventdefault:function(event) {    if (event.preventdefault) {      Event.preventdefault ();   } else {    & nbsp Event.returnvalue = false;   } },  stoppropagation:function (event) {    if ( event.stoppropagation) {      event.stoppropagation ();   } else {      event.cancelbubble = true;   }};

Block event bubbling

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.