【发布时间】:2015-08-19 16:15:19
【问题描述】:
我正在使用 html php 和 jquery 创建一个 5 星评级系统,当用户点击评级时,我不知道如何停止星评级。 在我的代码中,当用户点击 4 颗星时,警报框会显示 4 颗星,但是当用户将鼠标从星星上移开时,星星会显示 0 分。
这是我的代码,我不会在这里发布 css
HTML:
<div class="rating">
<div class="ratings_stars" data-rating="1"></div>
<div class="ratings_stars" data-rating="2"></div>
<div class="ratings_stars" data-rating="3"></div>
<div class="ratings_stars" data-rating="4"></div>
<div class="ratings_stars" data-rating="5"></div>
</div>
查询:
$('.ratings_stars').hover(
// Handles the mouseover
function() {
$(this).prevAll().andSelf().addClass('ratings_over');
$(this).nextAll().removeClass('ratings_vote');
},
// Handles the mouseout
function() {
$(this).prevAll().andSelf().removeClass('ratings_over');
}
);
$('.ratings_stars').click(function() {
$('.ratings_stars').removeClass('selected'); // Removes the selected class from all of them
$(this).addClass('selected'); // Adds the selected class to just the one you clicked
var rating = $(this).data('rating');
alert(rating);
// Get the rating from the selected star
$('#rating').val(rating); // Set the value of the hidden rating form element
});
【问题讨论】:
-
你能创建一个 jsfiddle 吗?
-
在不知道您期望发生什么的情况下无法回答。