我的代碼是:this.style.backgroundColor不工作IE7/8
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
p{
border:1px solid #CCC;
margin:5px;
padding:5px;
}
</style>
<script type="text/javascript">
window.onload = changeColor;
function changeColor() {
for(var i =0; i < document.getElementById("main").getElementsByTagName("p").length; i++) {
var obj = document.getElementById("main").getElementsByTagName("p")[i];
if (window.addEventListener) {
obj.addEventListener('mousemove', function() {
this.style.backgroundColor ="#EEE";
}, false);
obj.addEventListener('mouseout', function() {
this.style.backgroundColor ="#FFF";
}, false);
} else if (window.attachEvent) {
//for ie
obj.attachEvent('onmousemove', function() {
this.style.backgroundColor ="#EEE";
});
obj.attachEvent('onmouseout', function() {
this.style.backgroundColor ="#FFF";
});
}
}
}
</script>
</head>
<body>
<div>
<p>1</p>
<div id="main">
<p>2.1</p>
<p>2.2</p>
<p>2.3</p>
</div>
</div>
</body>
</html>
它在Chrome,火狐和IE9很好地工作,但在IE7中不起作用/ 8
該錯誤信息是:無法設置的「的backgroundColor」屬性值:對象爲null或undefined
什麼我榮?
附加了輔助功能,使'的attachEvent()'工作像'的addEventListener()'。 – jfriend00 2012-02-20 05:11:07