attachEvent与addEventListener
attachEvent:是IE支持的事件绑定方法
用法:元素节点对象.attachEvent(“on” + 事件名, 处理函数)
let qy = window.document.querySelector.bind(document) //节点选择器
qy("#id").attachEvent("on" + click, ()=>{
console.log("IE的事件绑定方法")
})
addEventListener:是IE支持的事件绑定方法
用法:元素节点对象.addEventListener(事件名, 处理函数,捕获或冒泡)
let qy = window.document.querySelector.bind(document) //节点选择器
qy("#id").addEventListener(click, ()=>{console.log("IE的事件绑定方法")},false)
处理兼容函数
在attachEvent与addEventListener两者间做选择
function addEvent(el, even, handler_cb){
if(window.attachEvent){
//如果支持attachEvent则输出该方法
return function(){
el.attachEvent("on" + even, handler_cb);
}
//如果只支持addEventListener则输出该方法,并传个type进行调用
}else if(window.addEventListener){
return function(type){
el.addEventListener(even, handler, type
)}
}
}