(function($){ $.fn.rating = function() { return this.each(function() { var rating = $(this).data('current'); rating = rating ? rating : 0; var stars = $(this).data('stars'); stars = stars ? stars : 5; for(var i = 1; i <= stars; i++) { var a = $('
', {html: '★'}); if(i <= rating) { a.addClass('set on') } $(this).append(a); } $(this).find('div').mouseenter(function(e) { var t = $(this); t.parent('.rating').find('div').removeClass('on'); t.prevAll().addClass('on'); t.addClass('on'); }); $(this).mouseleave(function(e) { var t = $(this); t.find('div.set').addClass('on'); t.find('div:not(.set)').removeClass('on'); }); $(this).click(function(e) { var t = $(this); t.find('div').removeClass('set'); t.find('div.on').addClass('set'); t.trigger('rated', [t.find('.on').length, t.find('div').length]) }); }); } })(jQuery);