JS Operation Dom dynamically add and remove events

Source: Internet
Author: User

Note that event names do not take on, such as onclick as click, under IE

Add event: Dom object. AddEventListener (' event name ', function name, true/false);

Delete Event: Dom object. RemoveEventListener (' event name ', function name, true/false);

The third parameter is Usecapture, and if true, the browser uses capture interception to handle the sequence of events, and if False, the browser handles the sequence of events in bubbing bubbling mode

is generally false

To give an example, you can modify the true or false to see the effect:

<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head>    <title></title>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/></Head><Body><DivID= "Div_test">div_test<inputtype= "button"ID= "Btn_test"value= "Btn_test"/></Div><Scripttype= "Text/javascript">window.onload= function() {document.getElementById ("div_test"). AddEventListener ("Click", Test1,false); document.getElementById ("btn_test"). AddEventListener ("Click", Test2,false); }    functiontest1 () {alert ("Outer DIV Trigger")    }    functiontest2 () {alert ("Inner input trigger")    }</Script></Body></HTML>
Example

IE, the event name can not go on, normal write

Add event: Dom object. Attachevent (' event name ', function name);

Delete Event: Dom object. DetachEvent (' event name ', function name);

Note that the function names that bind and delete events must be the same, and anonymous functions cannot be used, and anonymous functions cannot specify function names

Common code is as follows:

//Binding Eventsif(typeofDocument.addeventlistener! = "undefined") {Document.addeventlistener ("MouseDown", _lhlclick,true); } Else{document.attachevent ("OnMouseDown", _lhlclick); } //Unbind Eventif(typeofDocument.addeventlistener! = "undefined") {Document.removeeventlistener ("MouseDown", _lhlclick,true); } Else{document.detachevent ("OnMouseDown", _lhlclick); } 
binding and unblocking events

JS Operation Dom dynamically add and remove events

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.