jquery 商品評論 星星點選 事件
阿新 • • 發佈:2018-11-23
五角星可以在iconfont.cn上面找, 名字分別為:
五角星(滿)、五角星(空)

代表空星星

代表實心星星
html 程式碼
<div class="start_box">
<i class="iconfont"></i>
<i class="iconfont"></i>
<i class="iconfont"></i>
<i class ="iconfont"></i>
<i class="iconfont"></i>
</div>
jquery 程式碼
$(".start_box .iconfont").click(function() {
var parent = $(this).parent();
var tindex = $(this).index();
// 查詢上次最後一顆實心星星的位置
var lastStartPosition = 0;
var iconfont = parent.find('.iconfont');
for (var i = iconfont.length - 1; i >= 0; i--) {
if (iconfont.eq(i).html() == "") {
lastStartPosition = i;
break;
}
}
// 如果上次最後一顆實心星在第一位,並且這次點選的星星是第一顆,並且點選星星是實心的。那麼全部重置為空心星星。
// $(this).html() == "" 可能那個五角星看不到,可以試試console.log($(this).html());
if (lastStartPosition == 0 && tindex == 0 && $(this).html() == "") {
tindex = -1;
}
// 負責設定
parent.find('.iconfont').each(function(index, el) {
if (index <= tindex) {
$(this).html('');
} else {
$(this).html('');
}
});
});