W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <head> <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> </head> <body> <h3>本例演示了 onmousemove、onmouseleave 与 onmouseout 之间的区别。</h3> <p>每次鼠标指针移动到 div 元素上时都会发生 onmousemove 事件。</p> <p>mouseleave 事件仅在鼠标指针移出 div 元素时发生。</p> <p>onmouseout 事件在鼠标指针移出 div 元素时发生,且当它离开子元素(p 和 span)。</p> <div onmousemove="myMoveFunction()"> <p>onmousemove: <br> <span id="demo">请把鼠标悬停在我上面!</span></p> </div> <div onmouseenter="myEnterFunction()"> <p>onmouseenter: <br> <span id="demo2">请把鼠标悬停在我上面!</span></p> </div> <div onmouseover="myOverFunction()"> <p>onmouseover: <br> <span id="demo3">请把鼠标悬停在我上面!</span></p> </div> <script> var x = 0; var y = 0; var z = 0; function myMoveFunction() { document.getElementById("demo").innerHTML = z+=1; } function myEnterFunction() { document.getElementById("demo2").innerHTML = x+=1; } function myOverFunction() { document.getElementById("demo3").innerHTML = y+=1; } </script> </body> </html>