gpt4 book ai didi

javascript - 选择其父元素没有特定类的元素

转载 作者:行者123 更新时间:2023-11-29 18:33:17 25 4
gpt4 key购买 nike

假设,我有以下 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 代码:('mouseleave' 函数不起作用)

        $(".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');
});

最佳答案

$(this).parent() 选择 li 元素,所有其他函数都应用于此元素而不是 a 元素。

你可以这样做:

$(this).not(".active > a").stop()...

DEMO

关于javascript - 选择其父元素没有特定类的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5620560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com