JavaScript event delegate

Source: Internet
Author: User
Tags tag name tagname

Event delegate

Event delegate        function F2 (d) {            Document.onclick = function (e) {                var e = e | | window.event;                var target = E.srcelement | | E.target;                if (Target.tagname = = "LI") {...}}}      

  

Event Delegation: The event bubbling mechanism is used to add events to the parent to trigger the execution effect.

Benefits:

1. Improve performance

2. The newly added element will also have previous events

e.srcelement | | E.target: Gets the original DOM element when the event occurs.

Target.tagname: The first tag name of the DOM element, for example: LI;

Perform different processing functions by judging the attributes of the elements, such as tag name, ID, class, and so on.

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

In the example above: clicking on any of the LI will trigger the onclick event of document, (note that Li here does not bind any of the onclick events), so it can be understood as a "delegate".

Give the event to document. Let it handle the statements that Li should execute after the click.

JavaScript event delegate

Related Article

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.