这里的解决办法远比你大笑。 你可能发现,它以实际方式向你们教授许多更先进的技术。
我对《守则》作了相当广泛的评论。 让我知道,你是否有任何问题。
请注意,我的解决办法取决于@Abhi Beckert的关键观察,因此他应当获得接受的答复荣誉。
守则载于:。 http://jsfiddle.net/GtPnr/4/。
And here is the new HTML:
<div id="rating">
<table>
<tr>
<td><div id="1star" data-stars="1"></div></td>
<td><div id="2star" data-stars="2"></div></td>
<td><div id="3star" data-stars="3"></div></td>
<td><div id="4star" data-stars="4"></div></td>
<td><div id="5star" data-stars="5"></div></td>
</tr>
</table>
</div>
<input type="hidden" id="stars" />
我的之三者,但评论说, Java文:
// store re-used jQuery objects in variables to greatly improve performance.
// this avoids re-creating the same jQuery object every time it is used.
var $rating = $( #rating );
var $starsField = $( #stars );
// use the .hover() as a shortcut for mouseover and mouseout handlers.
$rating.find( td ).hover(function() {
// remove all star classes then dynamically construct class name to add
// using the data method to retrieve the value stored in the "data-stars" attribute
// I added.
$rating.removeClass( star1 star2 star3 star4 star5 ).addClass( star + $(this).find( div ).first().data( stars ));
}, function() {
// this is the mouse-out handler. Remove all star classes
$rating.removeClass( star1 star2 star3 star4 star5 );
// if the current value in the hidden field is set, then assign the correct class
// so the star s clicked value is respected.
var curVal = $starsField.val() || 0;
if (curVal > 0) {
$rating.addClass( star + curVal);
}
}).click(function() {
// when clicking a star, store the value in the hidden input
$starsField.val($(this).find( div ).first().data( stars ));
});