On the common methods of JS registration events _javascript Skills

Source: Internet
Author: User

Do not explain, look at the code:

Copy Code code as follows:

;(function () {
Carry out library encapsulation to prevent object contamination
window[' cm ']={};
/**
* Registering events for objects
*/
var addlistener=function (ELEMENT,TYPE,FN) {
if (typeof element = = ' undefined ') return false;
if (Element.addeventlistener) {
Element.addeventlistener (Type,fn,false);
}else if (element.attachevent) {
Buffer event to the label, resolved this point to window (now in FN to point to Element) and remove anonymous event problem
var _eventref= ' _ ' +type+ ' eventref ';
if (!element[_eventref]) {
Element[_eventref]=[];
}
var _eventrefs=element[_eventref];
var index;
For (index in _eventrefs) {
if (_eventrefs[index][' REALFN ']==fn) {
Return
}
}
var nestfn=function () {
Fn.apply (element,arguments);
};
Element[_eventref].push ({' REALFN ': FN, ' nestfn ': Nestfn});
Element.attachevent (' on ' +type,nestfn);
}else{
element[' on ' +type]=fn;
}
};
window[' cm ' [' AddListener ']=addlistener;
/**
* Remove registered events on object
*/
var removelistener=function (ELEMENT,TYPE,FN) {
if (typeof element = = ' undefined ') return false;
if (Element.removeeventlistener) {
Element.removeeventlistener (Type,fn,false);
}else if (element.detachevent) {
var _eventref= ' _ ' +type+ ' eventref ';
if (!element[_eventref]) {
Element[_eventref]=[];
}
var _eventrefs=element[_eventref]
var index;
var nestfn;
For (index in _eventrefs) {
if (_eventrefs[index][' REALFN ']==fn) {
nestfn=_eventrefs[index][' NESTFN '];
if (index==_eventrefs.length-1) {
Element[_eventref]=_eventrefs.slice (0,index);
}else{
Element[_eventref]=_eventrefs.slice (0,index). Concat (_eventrefs.slice (index+1,_eventrefs.length-1));
}
Break
}
}
if (NESTFN) {
Element.detachevent (' on ' +type,nestfn);
}
}else{
element[' on ' +type]=null;
}
};
})();

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.