国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > web前端 > js教程 > 正文

jquery阻止click與mouseover以及mouseout冒泡問題解決

黃舟
發(fā)布: 2017-06-28 14:21:17
原創(chuàng)
2160人瀏覽過

<ul>
                <li class="red"><a href="javascript:void(0)">項目一</a></li>
                <li class="green"><a href="javascript:void(0)">項目二</a></li>
                <li class="green"><a href="javascript:void(0)">項目三</a></li>
                <li class="green"><a href="javascript:void(0)">項目四</a></li>
            </ul>

 $(function () {
            $.each($("li"), function (i, o) {
                $(this).children("a").click(function () {
                    allhide();
                    $(this).parent("li").attr("class", "red");
                });
                if ($(this).attr("class") != "red") {
                    $(this).mouseenter(function () {
                        $(this).attr("class", "red");
                    });
                    $(this).mouseleave(function () {
                        $(this).attr("class", "green");
                    });
                }
            });
        });
        function allhide() {
            $.each($("li"), function (i, o) {
                $(this).attr("class", "green");
            });
        }
 <style type="text/css">
        li
        {
            width: 80px;
            list-style-type: none;
        }
        .red
        {
            background-color: Red;
        }
        .green
        {
            background-color: Green;
        }
    </style>
登錄后復制
 $.each($("li"), function (i, o) {
            $(this).children("a").click(function (e) {
                $("li").attr("class", "green");//不用另起函數(shù)
                $(this).parent("li").attr("class", "red");
                e.stopPropagation();
            });
            if ($(this).attr("class") != "red") {
                $(this).mouseenter(function (e) {
                    $(this).attr("class", "red");
                    e.stopPropagation();
                });
                $(this).mouseleave(function (e) {
                    $(this).attr("class", "green");
                    e.stopPropagation();
                });
            }
        });
登錄后復制

好像不起作用,還是鼠標點擊時改變這個li的樣式,但是鼠標移開時就去掉了這個樣式了,不知道是什么原因

? ?

<script type="text/javascript">
    //這里不存在冒泡呀
    $(function () {
        $("li").each(function(){
            $(this).find("a").click(function(){
                $("li").attr("class","green");
                $(this).parent().attr("class","red");
            })
        });
    });
</script>
登錄后復制

哦,不是冒泡嗎??那我這里的點擊后移開鼠標樣式就改變了啊

$(this).children("a").click(function () {
                    allhide();
                    $(this).parent("li").attr("class", "red");
                });
登錄后復制

感覺是代碼的問題,你的$(this)指針變過了下面還能用嗎?
你用變量記錄一下var?that?=?$(this)試試

 if ($(this).attr("class") != "red") {
                    $(this).mouseenter(function () {
                        $(this).attr("class", "red");
                    });
                    $(this).mouseleave(function () {
                        $(this).attr("class", "green");
                    });
                }
            });
登錄后復制

恩,我自己也搞明白了,鼠標點擊事件時,顏色為紅色的樣式已經(jīng)變化了,而我沒有把變化的指針保存下來,以下是我修改后的代碼:?

 nodeli = $("ul li:first");
            $.each($("li"), function (i, o) {
                $(this).click(function () {
                    allhide();
                    $(this).attr("class", "red");
                    nodeli = $(this);
                    //alert(nodeli.html());
                });
                $(this).mouseover(function () {
                    $(this).attr("class", "red");
                });
                $(this).mouseout(function () {
                    $(this).attr("class", "green");
                    $(nodeli).attr("class", "red");
                    // alert(nodeli.html());
                });
            });
登錄后復制

以上就是jquery阻止click與mouseover以及mouseout冒泡問題解決的詳細內(nèi)容,更多請關注php中文網(wǎng)其它相關文章!

最佳 Windows 性能的頂級免費優(yōu)化軟件
最佳 Windows 性能的頂級免費優(yōu)化軟件

每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓,幫助PHP學習者快速成長!
關注服務號 技術交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學習
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號