JQuery event propagation, event stream, and jquery event Propagation

Source: Internet
Author: User

JQuery event propagation, event stream, and jquery event Propagation

I. jQuery event Propagation

In the DOM2 event model, once an event is triggered, the event stream first spreads down from the top of the DOM tree (document node) until the target node, and then spread from the target node to the top of the DOM tree. The process from top to bottom is called the capture stage, and the process from bottom to top is called the bubble stage.

Use the loop body structure to register a capture type mouse-click class event handler function for the button element and all its parent nodes.

Sample Code:

<! DOCTYPE html> 

Next, modify the 3rd parameters of the addEventListener () method and set the parameter value to false, that is, to register the event as a bubble handler.

<Script type = "text/javascript"> window. onload = function () {var btn = document. getElementsByTagName ("input") [0]; // obtain the button var p = document. getElementsByTagName ("p") [0]; // p element var I = 1; // declare and initialize a temporary variable do {// use the do loop structure to register and click the event btn layer by layer. addEventListener ("click", function () {// register the mouse to click event p. innerHTML + = "<br/> (" + I ++ ")" + this. nodeName ;}, false); // dynamically tracks the name of the current response node. this. removeEventListener ("click", arguments. callee, false); // cancel the current mouse click event btn = btn. parentNode; // access the parent element at the upper level} while (btn); // set the cycle condition. If a parent node exists} </script>

Ii. jQuery event stream

Sample Code:

<! DOCTYPE html> 






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.