原生javascript兼容性总结
1.addEventListener() :方法用于向指定元素添加事件句柄。// Internet Explorer 8 及更早IE版本不支持,Opera 7.0 及 Opera 更早版本也不支持。
语法:element.addEventListener(event, function, useCapture);
解决方法:attachEvent() 方法来添加事件句柄
跨浏览器的解决方法:
var x = document.getElementById("myBtn");
if (x.addEventListener) { //所有主流浏览器,除了 IE 8 及更早 IE版本
x.addEventListener("click", myFunction);
} else if (x.attachEvent) { // IE 8 及更早 IE 版本
x.attachEvent("onclick", myFunction);
}
if (x.addEventListener) { //所有主流浏览器,除了 IE 8 及更早 IE版本
x.addEventListener("click", myFunction);
} else if (x.attachEvent) { // IE 8 及更早 IE 版本
x.attachEvent("onclick", myFunction);
}