简体   繁体   English

如何在课堂上获得prev / next元素

[英]How to get the prev/next element in class

I want to to fadeOut the previous and the next class of a specific one, why my code isnt working? 我想fadeOut上一个和下一个特定的类,为什么我的代码不工作? You know the solution? 你知道解决方案吗?

Note: How to specify that the previous of #homebut is #aboutus, and that the next of#aboutus is #homebut? 注意:如何指定#homebut的前一个是#aboutus,#aboutus的下一个是#homebut?

Thanks a lot for any help! 非常感谢您的帮助!

This is my html-code: 这是我的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>

and this my javascript till now: 这是我的javascript直到现在:

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

My first thoughts, would be: 我的第一个想法是:

$('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 . JS小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM