Home  >  Article  >  Web Front-end  >  JS event binding function code_javascript skills

JS event binding function code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:28:27738browse
Mainly solved

Browser compatibility, now compatible with IE6 7 8 FF Google (nonsense)
In IE browser, this points to the problem.
Just go to the code!
Copy code The code is as follows:

var bind=function(object,type,fn){
if(object.attachEvent){//IE browser
object.attachEvent("on" type,(function(){
return function(event){
window.event.cancelBubble= true;//Stop time bubbling
object.attachEvent=[fn.apply(object)];//----What I want to talk about here is here
}
})(object), false);
}else if(object.addEventListener){//Other browsers
object.addEventListener(type, function(event){
event.stopPropagation();//Stop time bubbling
fn.apply(this)
});
}

}

//The following is a click event added to the ID AAA
bind(document .getElementById("aaa"),"click",function(){alert("This is the ID of the button you clicked" this.id "This is the first bound function")});
bind (document.getElementById("aaa"),"click",function(){alert("This is the ID of the button you clicked" this.id "This is the second bound function")});

The code is very simple and does not require any explanation. Just use it and you will know. hehe.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn