[英]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? 我该如何实现?
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.