<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)其它相關文章!
每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號