我有這樣的代碼,鼠標懸停工作正常,但點擊時無法正常工作jQuery的鼠標懸停和點擊不工作相同的元素
HTML代碼:
<span class="rating">
<span class="rating1 rate" data-rating="1" title="1"><i class="icon-star-empty" ></i> </span>
<span class="rating2 rate" data-rating="2" title="2"><i class="icon-star-empty" ></i></span>
<span class="rating3 rate" data-rating="3" title="3"><i class="icon-star-empty" ></i></span>
<span class="rating4 rate" data-rating="4" title="4"><i class="icon-star-empty" ></i></span>
<span class="rating5 rate" data-rating="5" title="5"><i class="icon-star-empty" ></i></span>
JQUERY
$(document).on('mouseover', '.rate', function() {
var rating = parseInt($(this).attr('data-rating'), 10);
var rate = rating + 1;
for (var j = 1; j < rate; j++)
{
$('.rating' + j).html('<i class="icon-star" title="' + j + '" data-rating="' + j + '"></i>');
}
for (var i = rate; i < 6; i++)
{
$('.rating' + i).html('<i class="icon-star-empty" title="' + i + '" data-rating="' + i + '"></i>');
}
});
$(document).on('click', '.rate', function() {
var rate = parseInt($(this).attr('data-rating'), 10);
alert(rate);
});
JSFIDDLE http://jsfiddle.net/code_snips/EnjCH/1/
@SRy你確定嗎?這兩個事件可以共存於相同的元素... – mlnyc
不知道什麼是不工作...我去了jsfiddle,它似乎都很好... – buzzsawddog
@buzzsawddog在Js小提琴嘗試點擊明星。警報不發射 – Manish