首頁 > web前端 > js教程 > 主體

jQuery mouseenter方法的不冒泡如何理解?

黄舟
發布: 2017-06-28 09:51:18
原創
1084 人瀏覽過

程式碼:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQ--mouseenter测试</title>
<style>
*{margin: 0; padding: 0; color: #fff;}
div{width: 100px; height: 100px; padding-top: 20px; background: green; margin: 100px auto;}
p{width: 150px; height: 50px; background: red;}
</style>
<script src="jquery.js"></script> <!-- 1.9.0版 -->
</head>
<body>
    <div id="d">
        <p>子元素</p>
        父元素
    </div>
<script>
var a=0;
$(function(){ 
    $(&#39;#d&#39;).mouseenter(function(){    /*#d,父元素,当mouseenter时背景变为黑色*/ 
        $(this).css(&#39;background&#39;,&#39;#000&#39;);
    });
    $(&#39;#d p&#39;).mouseenter(function(){    /*#d p,子元素,当mouseenter时背景变为黑色*/      
        $(this).css(&#39;background&#39;,&#39;#000&#39;);
    });    
});
登入後複製

**當滑鼠移入子元素時,我以為只有子元素的背景變成黑色,因為.mouseenter()不冒泡,但結果父、子兩個元素都變成黑色了。我不懂了,mouseenter不冒泡啊,那應該只有子元素背景變成黑色,怎麼都變了?求講解! **

1)mouseenter不冒泡是指,當以一個元素捕獲到mouseenter事件後,不再通知其父元素,父元素也就不會處理mouseenter時間了2)就問題中的2個mouseenter

//只要元素进入div#d区域,mouseenter事件就会被触发
//一个mouse要进入一个子元素,必然经过其父元素,故鼠标达到子元素触发mouseenter后其父元素的mouseenter事件也会被触发
$(&#39;#d&#39;).mouseenter(function(){    /*#d,父元素,当mouseenter时背景变为黑色*/ 
    $(this).css(&#39;background&#39;,&#39;#000&#39;);
});
//只要元素进入div#d p区域,也就是div#d的子元素p区域是,mouseenter事件就会被触发
//但是此时div#d上的mouseenter事件监听不会再被触发,因为没有冒泡上去
$(&#39;#d p&#39;).mouseenter(function(){    /*#d p,子元素,当mouseenter时背景变为黑色*/      
    $(this).css(&#39;background&#39;,&#39;#000&#39;);
});
登入後複製

似乎還是不好理解那麼我們就看下冒泡的mouseover是個什麼樣的表現

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="../script/jquery-2.1.3.min.js"></script>
    <script>
        $(function(){
            /*
            $(&#39;#d&#39;). mouseover(function(){
                $(this).css(&#39;background&#39;,&#39;orange&#39;);
                if(!$(this).data(&#39;data-count&#39;)){
                    $(this).data(&#39;data-count&#39;,1)
                }else{
                    $(this).data(&#39;data-count&#39;,$(this).data(&#39;data-count&#39;)+1);
                }
                $(this).find(&#39;span&#39;).text($(this).data(&#39;data-count&#39;));
            });

            $(&#39;#d p&#39;).mouseover(function(){
                $(this).css(&#39;background&#39;,&#39;yellow&#39;);
                if(!$(this).data(&#39;data-count&#39;)){
                    $(this).data(&#39;data-count&#39;,1)
                }else{
                    $(this).data(&#39;data-count&#39;,$(this).data(&#39;data-count&#39;)+1);
                }
                $(this).text($(this).data(&#39;data-count&#39;));
            });
            */
            $(&#39;#d&#39;). mouseenter(function(){
                $(this).css(&#39;background&#39;,&#39;orange&#39;);
                if(!$(this).data(&#39;data-count&#39;)){
                    $(this).data(&#39;data-count&#39;,1)
                }else{
                    $(this).data(&#39;data-count&#39;,$(this).data(&#39;data-count&#39;)+1);
                }
                $(this).find(&#39;span&#39;).text($(this).data(&#39;data-count&#39;));
            });

            $(&#39;#d p&#39;).mouseenter(function(){
                $(this).css(&#39;background&#39;,&#39;yellow&#39;);
                if(!$(this).data(&#39;data-count&#39;)){
                    $(this).data(&#39;data-count&#39;,1)
                }else{
                    $(this).data(&#39;data-count&#39;,$(this).data(&#39;data-count&#39;)+1);
                }
                $(this).text($(this).data(&#39;data-count&#39;));
            });

        });

    </script>
</head>
<body>
<div id="d" style="width:200px;height:200px;background-color: greenyellow">
    <span>父元素</span>
    <p style="padding:5px;width:100px;height:100px;background-color: red;display:block">子元素</p>
</div>
</body>
</html>
登入後複製

運行上面餓代碼我們能看到,每次滑鼠進入子元素觸發mouseover事件,就會冒泡到其父元素,進入執行父元素的mouseover事件監聽-顯示的數字會增加
而換成mouseenter事件監聽就不會發生這樣的情況-顯示的數字不增加
並且在子元素的mouseover的事件監聽函數中阻止冒泡-stopPropagation,父元素的事件就不會被回調

以上是jQuery mouseenter方法的不冒泡如何理解?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!