Because for browsers that support DOM, adding events is using the AddEventListener () method to add events to the Object!
For MSIE, the attachevent () is used to add events to the Object! This allows us to use a container to load the two different browsers on how the event is handled! So we can simply call the Addevent () method to add an event to the object!
This is not more convenient?! Oh......
Let's take a look at it!
/**
* Register a listener event to the element
* @param the object to which the event is to be added by {Object} node
* @param {Object} type event types
* @param {Object} Listener event method
*/
function addevent (node, type, listener) {
Use the previous method to check for compatibility to ensure smooth degradation
if (!) ( node = $ (node)) return false;
if (node.attachevent) {//This is a method for IE
node[' e ' +type+listener] = listener;
Node[type+listener] = function () {node[' E ' +type+listener] (window.event);}
Node.attachevent (' On ' +type, Node[type+listener]);
return true;
}else if (node.addeventlistener) {
This is a method for browsers that support DOM
Node.addeventlistener (type, listener, false);
return true;
}
Returns false if neither of the methods is available;
return false;
};
window[' liujingning ' [' addevent '] = addevent;
How to use:
For example, to add an event to the onload () of the page, we can write this:
Liujingning.addevent (window, ' Load ', function (Event) {//write the code you want to write here}
We can also add events to an ID
var getId = document.getElementById (' AA ');
Liujingning.addevent (getId, ' Load ', function (Event) {//write the code you want to write here}
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