html中给div设置onmouseover事件 这个代码为什么不对?
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!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" />
<script type="text/javascript">
document.getElementById("div").onmouseout = function onmouseout(obj){
obj.style.background = "blue";
}
document.getElementById("div").onmousemove = function onmouseover(obj) {
obj.style.background = "gray";
}
</script>
<title>测试w</title>
</head>
<body>
<div id = "div" style = "width: 200px;height: 200px;background: red;"></div>
</body>
</body>
</html> 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
document.getElementById("div").onmouseout = function onmouseout(obj){
obj.style.background = "blue";
}
document.getElementById("div").onmousemove = function onmouseover(obj) {
obj.style.background = "gray";
}
</script>
<title>测试w</title>
</head>
<body>
<div id = "div" style = "width: 200px;height: 200px;background: red;"></div>
</body>
</body>
</html> 展开
1个回答
展开全部
大概有几个问题吧
1,getElementById是在文档节点加载完成之后才能获取到的,所以你这个代码即使没错误也会提示你 null,所以应把script代码放到div下面
2,getElementById本来就是一个object了,所以后面的function不需要传入obj,里面直接用this即可
document.getElementById("div").onmouseout = function onmouseout(){
this.style.background = "blue";
}
3,</body>标签有两个,不认真的家伙
1,getElementById是在文档节点加载完成之后才能获取到的,所以你这个代码即使没错误也会提示你 null,所以应把script代码放到div下面
2,getElementById本来就是一个object了,所以后面的function不需要传入obj,里面直接用this即可
document.getElementById("div").onmouseout = function onmouseout(){
this.style.background = "blue";
}
3,</body>标签有两个,不认真的家伙
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询