使用onmouseover和onmouseout事件在鼠标第一次移动上去和第一次离开时不起作用
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>HTML事件</title>
</head>
<body>
<input id="btnStart" type="button" value="开始" onmouseover="omm(this,'purple')" onmouseout="omo(this,'grey')">
<input id="btnOver" type="button" value="结束" onmouseover="omm(this,'yellow')" onmouseout="omo(this,'green')">
<script type="text/javascript">
var btnStart = document.getElementById('btnStart');
var btnOver = document.getElementById('btnOver');
function omm(obj, col) {
obj.onmouseover = function() {
this.style.color = col;
}
}
function omo(obj, col) {
obj.onmouseout = function() {
this.style.color = col;
}
}
</script>
</body>
</html>
正在回答
同学,你好。这是因为onmouseover和onmouseout事件写了两遍造成的影响,如图:

建议js函数里不需要写onmouseover和onmouseout事件,修改如下:

祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星