[英]Fill up previous stars on a JQuery 5-star rating onmouseover system
我要实施5星评级系统,并把鼠标悬停位置剩下的星星填充为黄色(类称为yellow
),否则填充为grey
。
到目前为止,我已经做到了:
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的:
$("[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));
});
但这仅将一颗星星填充为黄色。 我需要以某种方式选择并填充所有先前的单个<i>
元素。 我该如何实现?
请检查小提琴 。 这是你要找的东西吗?
$("#stars3 > i").hover(function() {
$(this).prevAll().addClass('yellow').removeClass('grey')
$(this).addClass('yellow').removeClass('grey')
$(this).nextAll().addClass('grey').removeClass('yellow')
});
像这样:
$("[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.