gpt4 book ai didi

javascript - 如何获取类中的上一个/下一个元素

转载 作者:行者123 更新时间:2023-12-02 19:23:10 25 4
gpt4 key购买 nike

我想淡出特定类的上一个和下一个类,为什么我的代码不起作用?你知道解决办法吗?

注意:如何指定#homebut的前一个是#aboutus,而#aboutus的后一个是#homebut?

非常感谢您的帮助!

这是我的 html 代码:

<body>
<div class="menuEintraege" id="2">
<li class="current"><a href="home.html" id="homebut">home</a></li>
<li><a href="apps.html"id="appsbut">apps</a></li>
<li><a href="hedone.html" id="hedonebut">hedone</a></li>
<li><a href="contact.html" id="contactbut">contact</a></li>
<li><a href="aboutus.html" id="aboutusbut">about us</a></li>
</div>
</body>

到目前为止,这是我的 javascript:

$("a").click(function() {
$(this).prev("li").fadeOut("fast");
$(this).next("li").fadeOut("fast");
}

最佳答案

我的第一个想法是:

$('li a:first').addClass('active');

$('button').click(function(e) {
e.preventDefault();
var that = this,
$that = $(that),
aEls = $('.menuEintraege a'),
aActive = $('.menuEintraege a.active')
.closest('li')
.index();
if (that.id == 'pre') {
$('.active').removeClass('active');
var pre = aEls.eq(aActive - 1);
var prev = pre.length ? pre : aEls.last();
prev.addClass('active');
}
else if (that.id == 'nxt') {
$('.active').removeClass('active');
var pre = aEls.eq(aActive + 1);
var prev = pre.length ? pre : aEls.first();
prev.addClass('active');
}
});​

JS Fiddle demo .

关于javascript - 如何获取类中的上一个/下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12298413/

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