简体   繁体   中英

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? You know the solution?

Note: How to specify that the previous of #homebut is #aboutus, and that the next of#aboutus is #homebut?

Thanks a lot for any help!

This is my html-code:

<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:

$("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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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