javascript事件绑定(attachEvent、addEventListener)及兼容性处理
<!doctype html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>attachEvent</title>
<script type="text/javascript">
window.onload=function(){
var oR=document.getElementById("rr");
var oY=document.getElementById("yy");
var oB=document.getElementById("bb");
if(oR.attachEvent){
oR.attachEvent("onmouseover",function(){
alert("ok");
});
oR.attachEvent("onmouseover",function(){
alert("你是猪吗");
});
}
else{
oR.addEventListener("mouseover",function(){
alert("ok");
});
oR.addEventListener("mouseover",function(){
alert("你是猪吗");
});
}
}
</script>
</head>
<body>
<div id="rr" style="width: 60px;height: 60px;border-radius: 60px;background: red;margin: 10px;"></div>
<div id="yy" style="width: 60px;height: 60px;border-radius: 60px;background: yellow;margin: 30px;"></div>
<div id="bb" style="width: 60px;height: 60px;border-radius: 60px;background: blue;margin: 50px;"></div>
</body>
</html>