JavaScript(js)监听兼容优化代码
关于js监听代码与兼容
//添加事件监听兼容函数
function addHandler(target, eventType, handler){
if(target.addEventListener){//主流浏览器
addHandler = function(target, eventType, handler){
target.addEventListener(eventType, handler, false);
};
}else{//IE
addHandler = function(target, eventType, handler){
target.attachEvent("on"+eventType, handler);
};
}
//执行新的函数
addHandler(target, eventType, handler);
}
// //删除事件监听兼容函数
// function removeHandler(target, eventType, handler){
// if(target.removeEventListener){//主流浏览器
// removeHandler = function(target, eventType, handler){
// target.removeEventListener(eventType, handler, false);
// }
// }else{//IE
// removeHandler = function(target, eventType, handler){
// target.detachEvent("on"+eventType, handler);
// }
// }
// //执行新的函数
// removeHandler(target, eventType, handler);
// }
addHandler(document.getElementById("xzlx"),"click",function(event){
var target=event.target;
if(target.value=="website"){
document.getElementById("wenzzh").style.display="none";
document.getElementById("wanzzh").style.display="";
}else if(target.value=="article"){
document.getElementById("wenzzh").style.display="";
document.getElementById("wanzzh").style.display="none";
}
});