<body>
<div id="box">
<input type="button" value="按钮" id="btn" onclick="showMes()" >
<input type="button" value="按钮2" id="btn2" >
<input type="button" value="按钮3" id="btn3" >
</div>
<script>
function showMes(){
alert('hello');
}
var btn2=document.getElementById('btn2');
var btn3=document.getElementById('btn3');
btn2.onclick=function(){
alert('这是一个DOM0级添加事件');
}
btn2.onclick=null;
//DOM二级事件
/* btn3.addEventListener('click',showMes,false);
btn3.addEventListener('click',function(){
alert(this.value);},false);
//删除事件
btn3.removeEventListener('click',showMes,false);
btn3.attachEvent('onclick',showMes);
btn3.detachEvent('onclick',showMes);*/
//跨浏览器事件
var eventUtil={
//添加句柄
addHandler:function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,hanlder,false);}
else if(element.attachEvent){
element.attachEvent("on"+type,handler);
}
else{
element['on'+type]=handler
}
},
//删除句柄
removeHandler:function(element,type,handler){
if(element.removeEventListener){
element.removeEventListener(type,hanlder,false);}
else if(element.detchEvent){
element.detchEvent('on'+type,handler);
}
else{
element['on'+type]=null;
}
}
}
eventUtil.addHandler(btn3,'click',showMes);
</script>
</body>
</html>
stone310
慕粉3968675