假設,我有以下HTML:選擇元素的父元素不具有特定的類
<ul class="topnav">
<li class=""><a href="/">Page 1</a></li>
<li class=""><a href="/Page2/">Page 2</a></li>
<li class=""><a href="/Page3/">Page 3</a></li>
<li class=""><a href="/Page4/">Page 4</a></li>
<li class=""><a href="/Page5/">Page 5</a></li>
<li class="active"><a href="/Page6/">Page 6</a></li>
</ul>
當鼠標離開LI元件,它是假設改變字體的顏色回到灰色除了父元素LI的類值爲「active」的A元素。
下面是jQuery代碼我想:(該「鼠標離開」功能無法正常工作)
$(".top_nav li a").mouseenter(
function() {
$(this).stop().animate({'color': '#ffffff'}, 'slow');
});
$(".top_nav li a").mouseleave(
function() {
$(this).parent().not(".active").stop().animate({'color': '#a5acb2'}, 'slow');
});
這個作品非常完美。謝謝。 – sidewinder 2011-04-11 11:29:59
它也值得看看.hover()函數,因爲它會壓縮你的代碼。 – 2011-04-11 11:31:37
這實際上很聰明,但它確實需要一點盯着:) +1 – karim79 2011-04-11 11:34:59