美文网首页
js鼠标事件onmouseover onmouseenter o

js鼠标事件onmouseover onmouseenter o

作者: 粥蛋蛋 | 来源:发表于2019-06-06 17:10 被阅读0次

    此案例可以明确三者之间的区别,有需要的可以把代码复制到本地编辑器查看并比对效果。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <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, onmouseenter 和 onmouseover 的不同。</h3> 
        <p> onmousemove 事件在鼠标移动到 div 元素上就开始时触发,在这个div上移动一直触发(冒泡)。</p> 
        <p> mouseenter 事件中有在鼠标指针进入 div 元素时触发(不冒泡)。 </p> 
        <p> onmouseover 事件在鼠标指针进入 div 元素时触发,在子元素上也会触发(p 和 span)(冒泡)。</p> 
        <hr>
        <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> 
    </body>
    <script>
        var x = 0,y = 0,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>
    </html>
    
    
    
    image.png

    相关文章

      网友评论

          本文标题:js鼠标事件onmouseover onmouseenter o

          本文链接:https://www.haomeiwen.com/subject/gdmuxctx.html