多个评论的印刷之星

时间:2019-03-14 07:24:56

标签: javascript jquery html

我使用此代码打印产品等级,但我想在同一页面上打印评论列表,因为我需要.each()方法,任何人都可以告诉我该如何实现。

  function generateRatingStars() {
    var rating = $('.fixture').text();
    var percent = Math.round(rating * 2) * 10;
    console.log(rating);
    function addScore(rating, $domElement) {
        $("<span class='stars-container'>")
                .addClass("stars-" + rating)
                .text("★★★★★")
                .appendTo($domElement);

    }
    ;

css代码为:         addScore(percent,$(“。fixture”));

   }


         .stars-container {
          position: relative;
          display: inline-block;
         color: transparent;
         }

        .stars-container:before {
         position: absolute;
         top: 0;
         left: 0;
         content: '★★★★★';
         color: lightgray;
        }

       .stars-container:after {
        position: absolute;
          top: 0;
          left: 0;
        content: '★★★★★';
        color: gold;
        overflow: hidden;
          }

HTML代码是:            

1 个答案:

答案 0 :(得分:0)

function generateRatingStars(){

    $.fn.stars = function () {
        return $(this).each(function () {

            var rating = parseFloat($(this).html());

            var percent = Math.round(rating * 2) * 10;

            var span = $("<span class='stars-container'>")
                    .addClass("stars-" + percent)
                    .text("★★★★★");
            $(this).html(span);

        });
    };
    $('span.stars').stars();
}