JS Event Monitoring

Source: Internet
Author: User

AddEventListener () method

element. AddEventListener (event, function, Usecapture);

The first parameter is the type of event (such as "click" or "MouseDown").

The second parameter is a function that is called after the event is triggered.

The third parameter is a Boolean value that describes whether the event is bubbling or capturing. This parameter is optional.

Note: Do not use the "on" prefix. For example, use "click" instead of "onclick".

"Hello world!" pops up when the user clicks on an element:

element. AddEventListener ("click", Function () {alert ("Hello world!");}); You can use the function name to refer to an external function:

"Hello world!" pops up when the user clicks on an element:

element. AddEventListener ("click", MyFunction);

function MyFunction () {
Alert ("Hello world!");
When passing a parameter value, use an anonymous function to invoke the function with the parameter: element. AddEventListener ("click", Function () {myFunction (P1, p2);});

The AddEventListener () method can specify the "usecapture" parameter to set the delivery type:

AddEventListener ( Event, function, usecapture);

The default is False, which is the bubbling pass, when the value is true, and the event uses capture delivery.

document.getElementById ("Mydiv"). AddEventListener ("Click", MyFunction, True); RemoveEventListener () method

The RemoveEventListener () method removes the event handle added by the AddEventListener () method:

Instance element. RemoveEventListener ("MouseMove", myFunction);

Cross-Browser Workaround:

var x = document.getElementById ("mybtn");
if (X.addeventlistener) {//All major browsers except IE 8 and earlier
X.addeventlistener ("click", MyFunction);
} else if (x.attachevent) {///IE 8 and earlier versions
X.attachevent ("onclick", myFunction);
}

JS Event 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.