JavaScript Remove event function code for DOM objects _javascript tips

Source: Internet
Author: User
JS Add/Remove events in IE and support DOM browsers are: attachevent (add event in IE), detachevent (delete event in IE), AddEventListener (add event in Dom browser), RemoveEventListener (Support for deletion events in the Dom browser).

For example, the first time you click on the black area when the pop-up warning, and remove the click event, that is, the second click of the time did not respond, the integration code is as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> Add Delete event </title> <script type=" Text/javascript "> var eventutil=new Object; Otarget: Target; Seventtype: event name; Funname: Event-triggered function name; Eventutil.addevent=function (otarget,seventtype,funname) {// document.getElementById ("DD"). attachevent if (otarget.addeventlistener) {//for DOM; Otarget.addeventlistener (Seventtype,funname, false); }else if (otarget.attachevent) {otarget.attachevent ("on" +seventtype,funname); }else{otarget["on" +seventtype]=funname; } }; Eventutil.removeevent=function (otarget,seventtype,funname) {//document.getelementbyid ("dd"). AttachEvent if ( Otarget.removeeventlistener) {//for DOM; Otarget.removeeventlistener (Seventtype,funname, false); }else if (otarget.detachevent) {Otarget.detaChevent ("on" +seventtype,funname); }else{otarget["on" +seventtype]=null; } }; function Removeclick () {Alert ("click"); var Odiv=document.getelementbyid ("Odiv"); odiv.style.cursor= "Auto"; Eventutil.removeevent (Odiv, "click", Removeclick); }//----Multiple functions are bound to the window.onload------//function Addloadevent (func) {var oldonload=window.onload; if (typeof window.onload!= "function") {Window.onload=func; }else{window.onload=function () {oldonload (); Func (); }} addloadevent (AddClick); function AddClick () {var Odiv=document.getelementbyid ("Odiv"); odiv.style.cursor= "Pointer"; Eventutil.addevent (Odiv, "click", Removeclick); </script> </pead> <body> <p> First click on the black area when the pop-up warning, and remove the click event, that is, the second click of the time did not respond to the </p> < Div id= "Odiv" > First point I warn you, the second time I ignore you! </div> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.