JQUERY traverses the problem.

the specific structure is as follows

html

<div class="list">
    <img src="" />
</div>
<div class="item">
    <div class="img active"><img src="images/01.jpg" /></div>
    <div class="img"><img src="images/02.jpg" /></div>
    <div class="img"><img src="images/03.jpg" /></div>
    <div class="img"><img src="images/04.jpg" /></div>
    <div class="img"><img src="images/05.jpg" /></div>
</div>
<div class="r-button"></div>

I wrote JQUERY

$(".r-button").on("click",function(){
    $(".item .img").each(function(){
        if($(this).is(".active")){
            var num = $(this).index() + 1;
            var img = $(".item .img").eq(num).children("img").attr("src");
            $(".item .img").eq(num).addClass("active").siblings().removeClass("active");
            $(".list img").attr("src",img);
        }
    });
});

problem description:
what I currently want to achieve is that clicking . R-button will get the next picture src and pass the value to img in .list DIV to display. And give the current .active to the switched .img DIV. Excuse me, what"s wrong with my JQUERY? Also hope to answer, thank you very much! ~

Sep.16,2021


$('.r-button').on('click',function(){
        var num = $('.item .img').hasClass('active').index();
        var el = $('.item .img').eq(num + 1);
        var src = el.find('img').attr('src');
        el.addClass('active').siblings().removeClass('active');
        if(src) {
            $('.list img').attr('src',src);
        }
    });

traversal is superfluous

Menu