Example of binding and unbinding functions in javascript

Source: Internet
Author: User

Mozilla:

How to Use addEventListener:

Target. addEventListener (type, listener, useCapture );

Target: document node, document, window, or XMLHttpRequest.
Type: String, event name, excluding "on", such as "click", "mouseover", and "keydown.
Listener: implements the EventListener interface or functions in JavaScript.
UseCapture: whether to use capture. Generally, false is used. For example, document. getElementById ("testText"). addEventListener ("keydown", function (event) {alert (event. keyCode) ;}, false );

IE:

Target. attachEvent (type, listener );
Target: document node, document, window, or XMLHttpRequest.
Type: String, event name, including "on", such as "onclick", "onmouseover", and "onkeydown.
Listener: implements the EventListener interface or functions in JavaScript. For example, document. getElementById ("txt"). attachEvent ("onclick", function (event) {alert (event. keyCode );});

W3C and IE support removing specified events at the same time. The purpose is to remove the specified events in the following format:

W3C format:

RemoveEventListener (event, function, capture/bubble );

The format of Windows IE is as follows:

DetachEvent (event, function );

Target. addEventListener (type, listener, useCapture );
Target file node, document, window, or XMLHttpRequest.
Type string, event name, excluding "on", such as "click", "mouseover", and "keydown.
Listener implements the EventListener interface or functions in JavaScript.
UseCapture: whether to use capture. After reading the event stream section, you will understand it. Generally, false is used.
When an Event is triggered, an Event object is passed to the Event handler, for example:
Document. getElementById ("testText"). addEventListener ("keydown", function (event) {alert (event. keyCode) ;}, false );
Different browser versions are applicable.
The attachEvent method is used in onclick IE.
The addEventListener method is used in click fox.
The usage principle of the two is as follows: the priority of the execution can be different. The example below is as follows:
The attachEvent method attaches other processing events to an event. (Mozilla series not supported)
The addEventListener method is used in the Mozilla series.
Example: document. getElementById ("btn"). onclick = method1;
Document. getElementById ("btn"). onclick = method2;
Document. getElementById ("btn"). onclick = method3; if so, only medhot3 will be executed.
Write as follows:
Var btn1Obj = document. getElementById ("btn1"); // object. attachEvent (event, function );
Btn1Obj. attachEvent ("onclick", method1 );
Btn1Obj. attachEvent ("onclick", method2 );
Btn1Obj. attachEvent ("onclick", method3); the execution sequence is method3-> method2-> method1
If the Mozilla series does not support this method, you need to use addEventListener var btn1Obj = document. getElementById ("btn1 ");
// Element. addEventListener (type, listener, useCapture );
Btn1Obj. addEventListener ("click", method1, false );
Btn1Obj. addEventListener ("click", method2, false );
Btn1Obj. addEventListener ("click", method3, false); execution sequence: method1-> method2-> method3
Example: (Note that the div must be placed before the js)

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.