鼠标悬停星形图标svg fontawesome

时间:2019-03-20 08:34:43

标签: svg mouseover font-awesome-5

将鼠标悬停在星号图标上时,星号的类别应更改,单击时应确定星号。脚本中存在错误,不允许这样做。

$('.comment-form-stars [data-fa-i2svg]').on('mouseover', function () {
            var onStar = parseInt($(this).data('value'), 10);
            $(this).parent().children('[data-fa-i2svg]').each(function (e) {
                if (e < onStar) {
                    $(this).removeClass('far');
                    $(this).addClass('fas');
                } else {
                    $(this).removeClass('fas');
                    $(this).addClass('far');
                }
            });

        }).on('mouseout', function () {
            if (!ratingVal || ratingVal == 0) {
                $(this).parent().children('[data-fa-i2svg]').each(function (e) {
                    $(this).removeClass('far');
                    $(this).addClass('fas');
                });
            } else {
                $(this).parent().children('[data-fa-i2svg]').each(function (e) {
                    var r = parseInt($(this).data('value'), 10);
                    if (r < ratingVal) {
                        $(this).removeClass('far');
                        $(this).addClass('fas');
                    }
                });
            }
        });
.fa-star {
      color: #f8c31d;
      cursor: pointer;
   }
<link href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<div class="comment-form-stars">
              <i class="fas fa-star" aria-hidden="true" data-value="1"></i>
              <i class="far fa-star" aria-hidden="true" data-value="2"></i>
              <i class="far fa-star" aria-hidden="true" data-value="3"></i>
              <i class="far fa-star" aria-hidden="true" data-value="4"></i>
              <i class="far fa-star" aria-hidden="true" data-value="5"></i>
</div>

1 个答案:

答案 0 :(得分:1)

您尝试在fontawesome将图标注入页面之前绑定到图标。通过绑定到父div并使用事件委托来避免这种情况,如here所述:

$('.comment-form-stars').on('mouseover', '[data-fa-i2svg]', function () {
            var onStar = parseInt($(this).data('value'), 10);
            $(this).parent().children('[data-fa-i2svg]').each(function (e) {
                if (e < onStar) {
                    $(this).removeClass('far');
                    $(this).addClass('fas');
                } else {
                    $(this).removeClass('fas');
                    $(this).addClass('far');
                }
            });

        }).on('mouseout', '[data-fa-i2svg]', function () {
            if (!ratingVal || ratingVal == 0) {
                $(this).parent().children('[data-fa-i2svg]').each(function (e) {
                    $(this).removeClass('far');
                    $(this).addClass('fas');
                });
            } else {
                $(this).parent().children('[data-fa-i2svg]').each(function (e) {
                    var r = parseInt($(this).data('value'), 10);
                    if (r < ratingVal) {
                        $(this).removeClass('far');
                        $(this).addClass('fas');
                    }
                });
            }
        });
.fa-star {
      color: #f8c31d;
      cursor: pointer;
   }
<link href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<div class="comment-form-stars">
              <i class="fas fa-star" aria-hidden="true" data-value="1"></i>
              <i class="far fa-star" aria-hidden="true" data-value="2"></i>
              <i class="far fa-star" aria-hidden="true" data-value="3"></i>
              <i class="far fa-star" aria-hidden="true" data-value="4"></i>
              <i class="far fa-star" aria-hidden="true" data-value="5"></i>
</div>