Attachevent can use anonymous functions, but after that, DetachEvent will not be able to uninstall them. The function that the detachevent unloads must use the function name.
The following code is not valid:
dom.attachevent (' onclick ', function () {alert ()});
Dom.detachevent (' onclick ', function () {alert ()});
The following code is also invalid:
Dom.attachevent (' onclick ', function () {alert ()});
Dom.onclick=null;
If the page element attribute is written dead, it can only be canceled using the form dom.onclick=null, and other dynamic additions and deletions may use the following extensions:
| The code is as follows |
Copy Code |
| /** * @description event bindings, compatible with each browser * @param target * Event Trigger Object * @param type * Events * @param func * Event handler function */ function bind (target, type, func) { if (Target.addeventlistener) {//Non IE and IE9 Target.addeventlistener (Type, func, false); else if (target.attachevent) {//IE6 to IE8 Target.attachevent ("On" + Type, func); } else { Target["on" + type] = func; Ie5 } } /** * @description event removal, compatible with each browser * @param target * Event Trigger Object * @param type * Events * @param func * Event handler function */ function unbind (target, type, func) { if (Target.removeeventlistener) { Target.removeeventlistener (Type, func, false); else if (target.detachevent) { Target.detachevent ("On" + Type, func); } else { Target["on" + type] = NULL; } } |
Example
| The code is as follows |
Copy Code |
| /* * Static page to cast the memory of the event added by this element. */ <input id= "Add" type= "button" value= "Add"/> <input id= "Det" type= "button" value= "Det"/> <script type= "Text/javascript" > (function () { var add = document.getElementById ("add"); var det = document.getElementById ("Det"); /* Add incident/* ~function () { var str = new Array (1000). Join (new Array (5000). Join ("xxxxx")); Add.onclick = function () { var arr = []; Arr.push (str); } }(); /* Removal Event/* ~function () { Det.onclick = function () { Add.onclick = null; } }(); })(); </script> |
Example
| The code is as follows |
Copy Code |
| /* * Dynamically added elements to cast memory. */ <input id= "Add" type= "button" value= "Add"/> <input id= "Det" type= "button" value= "Det"/> <div id= "box" ></div> <script type= "Text/javascript" > (function () { var add = document.getElementById ("add"); var det = document.getElementById ("Det"); var box = document.getElementById ("box"); /* Add incident/* ~function () { var str = new Array (1000). Join (new Array (5000). Join ("xxxxx")); Add.onclick = function () { var temp = document.createelement ("div"); temp.id = "Hello"; Temp.classname = "Hello" Box.appendchild (temp); Temp.onclick = function () { var arr = []; Arr.push (str); } } }(); /* Removal Event/* ~function () { Det.onclick = function () { document.getElementById ("Hello"). onclick = null; Add.onclick = null; } }(); })(); </script> |