简体   繁体   English

在鼠标悬停系统的JQuery 5星级评级中填满以前的星星

[英]Fill up previous stars on a JQuery 5-star rating onmouseover system

I want to implement a 5 star rating system and fill up the stars left from the mouseover position yellow (class called yellow ), otherwise grey . 我要实施5星评级系统,并把鼠标悬停位置剩下的星星填充为黄色(类称为yellow ),否则填充为grey

I have done this so far: 到目前为止,我已经做到了:

HTML: HTML:

<div id="stars3">
<i data-count="0" class="glyphicon glyphicon-star grey-light grey"></i> 
<i data-count="1" class="glyphicon glyphicon-star grey-light grey"></i> 
<i data-count="2" class="glyphicon glyphicon-star grey-light grey"></i> 
<i data-count="3" class="glyphicon glyphicon-star grey-light grey"></i> 
<i data-count="4" class="glyphicon glyphicon-star grey-light grey"></i> 
</div>

JQuery: JQuery的:

$("[id^=stars] > i").hover(function() {
    count = $(this).attr("data-count");
    $(this).each(function (i) {
        if ($(this).attr("data-count") < count)
        $(this).addClass("yellow");
    });
    console.log($(this));
});

But this only fills one single star to yellow. 但这仅将一颗星星填充为黄色。 I need to select and fill all previous single <i> elements somehow. 我需要以某种方式选择并填充所有先前的单个<i>元素。 How can I achieve this? 我该如何实现?

Please check the Fiddle . 请检查小提琴 Is this what were you looking for ? 这是你要找的东西吗?

$("#stars3 > i").hover(function() {
$(this).prevAll().addClass('yellow').removeClass('grey')
$(this).addClass('yellow').removeClass('grey')
$(this).nextAll().addClass('grey').removeClass('yellow')
});

Like this: 像这样:

 $("[id^=stars] > i").hover(function() { $(this).prevAll().addBack().toggleClass("yellow"); }); 
 .yellow { background-color:yellow } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="stars3"> <i data-count="0" class="glyphicon glyphicon-star grey-light grey">*</i> <i data-count="1" class="glyphicon glyphicon-star grey-light grey">*</i> <i data-count="2" class="glyphicon glyphicon-star grey-light grey">*</i> <i data-count="3" class="glyphicon glyphicon-star grey-light grey">*</i> <i data-count="4" class="glyphicon glyphicon-star grey-light grey">*</i> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM