下午複習昨天做的東西 結果在處理鼠標事件時出現了問題
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style>
#div1 {width:250px; height:250px; background:#C00; border:2px solid:#000;
display:none;}
</style>
<script type="text/javascript">
function change1()
{
var z=document.getElementById('div1').style;
z.display='block';
}
function change2()
{
**var z=document.getElementById('div1').style.display
z='none';**
}
</script>
</head>
<body>
<input type="checkbox" onmouseover="change1()" onmouseout="change2()"/>
<div id="div1">請注意隨身行李安全</div>
</body>
</html>
在處理onmouseover事件時運行正常,但是當處理onmouseout時出現了錯誤,我覺得錯誤在於,
var z=document.getElementById('div1').style.display
z='none';
這裏是對z進行了重新賦值,也就是說’none‘取代了document.getElementById('div1').style.display
而不是將它的值重新設置爲’none’,也就是說
z='nono';
使得
z=document.getElementById('div1').syle.display;
不再存在,被清除掉了,也就是說z在處理中無法指向
div1.style.display
所以纔會出現這樣的錯誤
-