Pregunta

Espero que alguien pueda ayudarme. ¿Cómo puedo hacer que AddClass se aplique solo al siguiente IMG y no a todos los seguidores? Editar: la "AddClass" siempre debe aplicarse al siguiente IMG siguiente para que pueda hacer clic en todas las imágenes una tras otra, como una galería

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

La UL con la ID "Galería" se ve así:

<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>
¿Fue útil?

Solución

Has probado :

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

fuente : http://api.jquery.com/first-selector/

Otros consejos

Tal vez podrías intentarlo

$("#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');
    }
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top