addClass должен применяться только к следующему IMG - PullRequest
2 голосов
/ 19 февраля 2012

Я надеюсь, что кто-нибудь может мне помочь.как сделать addClass относится только к следующему img, а не ко всем последующим?edit: «addClass» всегда должен применяться к следующему IMG, чтобы вы могли кликать по всем изображениям одно за другим - как галерея:

<li>
    <img class="toppicture" src="images/w1.jpg" title="Title #0"/>
</li>
<li>
    <img  src="images/w2.jpg" title="Title #1"/>
</li>
<li>
    <img  src="images/w3.jpg" title="Title #2"/>
</li>

Ответы [ 2 ]

1 голос
/ 19 февраля 2012

Вы пробовали:

$("#next").click(function(){
                if($("#gallery li").next("#gallery li").length != 0) {
                    var obj = $(".toppicture").parent("li").next().children("img");
                    $("#gallery li").children("img").removeClass('toppicture');
                    $(obj).addClass('toppicture');
                }
                else {
                    $(this).children("img").removeClass('toppicture');
                    $("#gallery li:first-child").children("img").addClass('toppicture');
                }
            });

источник: http://api.jquery.com/first-selector/

0 голосов
/ 19 февраля 2012

Может быть, вы могли бы попробовать

$("#next").click(function(){
    //find the li with the class
    var liTop = $("#gallery li.toppicture");
    //if it has another element remove the class from itself and add it to the next 
    if(liTop.next().length !== 0) {
        liTop.removeClass('toppicture');
        liTop.next().addClass('toppicture');
    }
    else {
        $(this).children("img").removeClass('toppicture');
        $("#gallery li:first-child").children("img").addClass('toppicture');
    }
});
...