简体   繁体   中英

Jquery: get stars that have index less than the current clicked star

I have a div with stars:

在此处输入图像描述

<aside class="rate review-rate">
                <span class="review-star" index="1">☆</span>
                <span class="review-star" index="2">☆</span>
                <span class="review-star" index="3">☆</span>
                <span class="review-star" index="4">☆</span>
                <span class="review-star" index="5">☆</span>
            </aside>

and a script that makes the star full when it's clicked:

$('.review-star').on('click', function () {
   /* desc = $(this).attr('data-desc'); //get the description
    $('#content').text(desc);*/
    alert("yes");
    alert($(this).attr("index"));
    $(this).text("★")

    var stars = $(".review-star").filter((index, el) => {
        return parseInt($(el).attr("index")) > $(this).attr("index");
    });
    console.log(stars);
    console.log(stars.length)
    for (var starsKey in stars) {
        //$(starsKey).text("★");
    //    console.log(starsKey);
    }
});

However I need to make the previous stars full as well. I tried getting all the stars with attribute index value less than that of the $(this).attr("index") but it didn't work.

EDIT : The answers with prevAll are awesome, but if someone could point me as to what I was doing wrong in my code, it would be very appreciated!

To achieve what you require you can simply use the prevAll() method. addBack() can also be used to include the element which was clicked. Try this:

 $('.review-star').on('click', function() { $(this).prevAll().addBack().text("★"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <aside class="rate review-rate"> <span class="review-star" index="1">☆</span> <span class="review-star" index="2">☆</span> <span class="review-star" index="3">☆</span> <span class="review-star" index="4">☆</span> <span class="review-star" index="5">☆</span> </aside>

You can try using prevAll() and nextAll()

 $('.review-star').on('click', function () { /* desc = $(this).attr('data-desc'); //get the description $('#content').text(desc);*/ //alert("yes"); //alert($(this).attr("index")); $(this).text("★") //var stars = $(".review-star").filter((index, el) => { // return parseInt($(el).attr("index")) > $(this).attr("index"); //}); //console.log(stars); //console.log(stars.length) $(this).prevAll().text("★"); $(this).nextAll().text("☆"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <aside class="rate review-rate"> <span class="review-star" index="1">☆</span> <span class="review-star" index="2">☆</span> <span class="review-star" index="3">☆</span> <span class="review-star" index="4">☆</span> <span class="review-star" index="5">☆</span> </aside>

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