JS Event binding/monitoring

Source: Internet
Author: User

Method of event binding/listening 1. Direct binding

As the name implies, it is directly bound to the DOM element onclick, onmouseover, onmouseout, onmousedown, onmouseup, ondblclick, onkeydown, onkeypress, OnKeyUp and other events

var ul = document.getElementById (' ul 'function() {    console.log (' Click event Binding succeeded ')}

This method is the simplest and is one of the earliest supported methods of Dom Level0. But there is a big problem with this approach. That is, if an element binds an event, it is possible to overwrite the previously bound event! Especially when there are multiple JS files. To solve this problem, Level2 has added event monitoring

2. Event Monitoring

The function of event listener implementation is similar to that of direct binding, but a new feature is added. That is, no matter the listening time, it will not overwrite the previous listener events. The essential reason is that the listener event produces a completely new anonymous function every time, unlike the previous function, which is naturally not overwritten.

var ul = document.getElementById (' ul ')        ul.addeventlistener (function() {            Console.log (' event binding succeeded 1 ')        })        Ul.addeventlistener (function() {            Console.log (' event binding succeeded 2 ')        })

JS Event binding/monitoring

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.