事件绑定
window.onload = function(){
var btn = document.getElementById("btn")
bind(btn,"click",function(){
console.log(this)
}
bind(btn,"click",function(){
console.log(this)
})
function bind(el,event,callback){
if(el.addEventListener){
el.addEventListener(event,callback,false)
}else{
el.attachEvent("on"+event,function(){
callback.call(el)
})
}
}
}
事件的传播
window.onload = function(){
var box1 = document.getElementById("box1")
var box2 = document.getElementById("box2")
var box3 = document.getElementById("box3")
bind(box1,"click",function(){
alert("box1")
})
bind(box2,"click",function(){
alert("box2")
})
bind(box3,"click",function(){
alert("box3")
})
function bind(el,event,callback){
if(el.addEventListener){
el.addEventListener(event,callback,true)
}else{
el.attachEvent("on"+event,function(){
callback.call(el)
})
}
}
}