JavaScript Event dissemination and bubbling

Javascript Event(事件)的传播与冒泡 Standard browser and IE9 + browser support the bubbling and capture of events, and IE8-browser only supports bubbles

    Standard and IE9 + browser use stopPropagation () or Cancelbubble blocking event propagation, and IE8-uses the E.cancelbubble property to blook the fashing, note that IE9 does not support CancelBubble properties (after setting Don’t take effect), but Chrome, Safari, Opera, Firefox supports the Cancelbubble property.
  • IE8-Add an event with Attachevent to DOM elements, but must be added over before the event name, such events can only be in the bubbling stage of the element.
  • The stoppropagatin () method is used to prevent the communication of the event. If set in the capture phase, the target and bubbling phase will not be executed;
  • Cancelbubble property can only prevent the bubbling stage, Events in the capture and target phase cannot block
  • preventdefault () and Window.Event.ReturnValue can prevent the default events for standard browsers and IE9 +. IE8- can use returnvalue, preventdefault ().
  • Example code (IE8-example is not available)
HTML code

Buttons
  stoppropagation  
Cancelbubble
CONT blocks arrestGet or bubble
Holiday relationship: body-> wrap-> cont-> Button, you can control the principles above JS code
$ (Function () {var $ log = $ ('. Log'), $ wrap = $ ('. Wrap'), $ Cont = $ ('.und'), $ btn = Document.GtelementByid ('btn'), $ stoptype = $ ('# stoptype'), $ body = $ ('body' ), $ intxt = $ ('# tent'), $ btnreject = $ ('# btnreject'); var ephase = ["", "capture", "target", "bubble"] var setBordercolor = function ($ DOM, Color, Time, Event) {$ DOM = $ ($ DOM); $ log.html ($ log.html () + $ dom.attr ('Class') + '[' + Ephase [Event.Eventphase] + ']' + '
') var timeindex = window.settimeout (Function () {$ DOM.CSS ({'bordercolor': color, 'borderwidth': '4px'});}, time);} //Capture $ body [0] .addeventListener ('Click', Function (Event) {$ log.html ($ log.html () + "------------------- "); setBorderColor ($ BODY, '# 0866FF', 0, Event);}, true); $ wrap [0] .addeventListener ('Click', Function (Event) {setBorderColor ($ WRAP, 'YELLOW ', 2000, Event);}, true); $ cont [0] .addeventListener (' click ", function (event) {event = event || window.event; if ($ stoptype.val () == '1 ') {Event.StopPropagation ();} else {Event.cancelbubble = true;} setbordercolor ($ Cont,' Green ', 1000, Event);}, true); $ btn.addeventListener (' click ", function ) {setBorderColor ($ BTN, 'Red', 0, Event);}, true); $ btnreject [0] .addeventlistener ('click', Function (Event) {setBorderColor ($ btnreject, 'gray', 0, Event );}, true); // Bubbling $ body [0] .addeventListener ('Click', Function (Event) {setBorderColor ($ Body, '# 0866ff ", 0, Event);}, false; $ wrap [0 ] .addeventListener ('Click', Function ($ WRAP, 'YELLOW', 2000, Event);}, false; $ cont [0] .addeventListener ('Click', Function (Event) {setBorderColor ($ CONT, 'GREEN', 1000, Event;}, false; $ btn.addeventListener ('Click', Function (Event) {setBorderColor ($ BTN, 'Red', 0, Event);}, false $ btnreject [0] .addeventListener ('Click', Function (Event) {setBorderColor ($ btnreject, 'gray', 0, event);}, false; // Block default event $ INTXT.KEYPRESS (Function) ) {//event.preventdefault (); window.event.returnValue = false; $ body.Append (String.Fromcharcode (esycode);});});
Implementing a complete EVENT stream DEMO

There is a code to prevent event propagation at the CONT capture event

Prevent the default event only for verification

  

application scenario
    The event application scenario in the capture phase is small, and it is used in the target and bubbling phase.
  1. Standard events in the current phase W3C have been generally supported, and if it is not compatible with IE8-browser, some compatibility code can be discarded.
  2. The above is all of this article, I hope this paper will bring certain help to everyone’s learning or work, and I hope to support Tumi cloud!
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment