Examples of adding and removing JavaScript events

Source: Internet
Author: User

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>

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.