Javascript-event Delegate

Source: Internet
Author: User

// Event Delegate
Function f2 (d ){

Document. onclick = function (e ){

Var e = e | window. event;
Var target = e. srcElement | e.tar get;

If (target. tagName = "LI "){
......
}
}
}
Event delegation: bind a processing function to the dom object that triggers the event at the top layer using the event bubbling mechanism. You can use event delegation when many dom objects are required to bind events.

E. srcElement | e.tar get: gets the first dom element when an event occurs.

Target. tagName: The Tag Name of the original dom element, for example, LI;

 

Different processing functions are executed by determining element attributes, such as tag names, IDs, and classes.

This is much more efficient than binding an event to each element and then binding a function to each event.

 

In the preceding example, clicking any LI will trigger the onclick event of the document. (Note that LI is not bound to any onclick event ".

Hand over the event to document. Let it process the statement that should be executed after the li click.

 

From Captain op

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.