【发布时间】:2011-11-13 02:04:57
【问题描述】:
我试图弄清楚如何编写一个 javascript 函数,该函数接受一个元素 + 一个分数并使用 jQuery addClass 和 removeClass 显示正确的星级评分(四舍五入到最接近的半星)但有问题...当我使用“全星”系统时,我使用了这个系统:
function() {
$(this).prevAll().andSelf().addClass('active');
$(this).nextAll().removeClass('active');
}
这不再适用于半星系统。有人有建议吗?这是我现在使用的代码:
function populateStars(stars, score) {
// Stars refers to the <ul> container with <li> stars inside (see later code).
// This function should use stars (container) and score (rating between 1-5) to display stars
}
这是“stars”变量所指的元素的示例:
<ul class="big-stars">
<li class="star-1 full">1</li>
<li class="star-2 full">2</li>
<li class="star-3 half">3</li>
<li class="star-4">4</li>
<li class="star-5">5</li>
</ul>
以及控制星星是满、空还是半满的 CSS。
.big-stars li {
text-indent: -9999px;
width: 49px;
height: 46px;
background: url('../images/big_star_empty.png');
}
.big-stars .full {
background: url('../images/big_star_full.png');
}
.big-stars .half {
background: url('../images/big_star_half.png');
}
【问题讨论】:
标签: javascript jquery html css