日期:2014-05-16  浏览次数:20416 次

js中的事件,问题
<script>
function   handleEvent(oEvent){
if(oEvent.type== "click "){
alert( "click ");
}else   if(oEvent.type== "mouseover "){
alert( "Mouse   Over ");
}else{
alert( "aaaa ");
}


}
var   oDiv=document.getElementById( "div1 ");
oDiv.onclick=handleEvent;//这行报错.
oDiv.onmouseover=handleEvent;
</script>

<div   id= "div1 "> aaaaaaaa
</div>

为什么提示oDiv是null或不是个物件呢.

------解决方案--------------------
HTML是顺序读取文件的.你把 "DIV1 "对象放在下面,当加载页面时是首先读取脚本的.
oDiv.onclick=handleEvent;//这行报错.又没放在函数块里,自然读不到什么对象了

------解决方案--------------------
hehe -物件-
<script>
function handleEvent(oEvent){
if(oEvent.type== "click "){
alert( "click ");
}else if(oEvent.type== "mouseover "){
alert( "Mouse Over ");
}else{
alert( "aaaa ");
}
}
var oDiv=document.getElementById( "div1 ");
oDiv.onclick=function(){handleEvent(event)};
oDiv.onmouseover=function(){handleEvent(event)};
</script>