简单教程
提交运行
代码编辑器:
<style> div { width: 100px; height: 100px; border: 1px solid black; margin: 10px; float: left; padding: 30px; text-align: center; background-color: lightgray; } p { background-color: white; } </style> <h3>演示了 onmousemove, onmouseleave 和 onmouseout 的区别</h3> <p> onmousemove 事件在鼠标移动到 div 元素上时触发</p> <p> mouseleave 事件只在鼠标指针移出 div 元素时触发。 </p> <p> onmouseout 事件在鼠标指针移出 div 元素及离开子元素(p 和 span)时触发</p> <div onmousemove="myMoveFunction()"> <p>onmousemove: <br> <span id="demo">鼠标移入和移出!</span></p> </div> <div onmouseleave="myLeaveFunction()"> <p>onmouseleave: <br> <span id="demo2">鼠标移入和移出!</span></p> </div> <div onmouseout="myOutFunction()"> <p>onmouseout: <br> <span id="demo3">鼠标移入和移出!</span></p> </div> <script> x = 0; y = 0; z = 0; function myMoveFunction() { document.getElementById("demo").innerHTML = z+=1; } function myLeaveFunction() { document.getElementById("demo2").innerHTML = x+=1; } function myOutFunction() { document.getElementById("demo3").innerHTML = y+=1; } </script>
运行结果: