I tried to build up a simple star rate render function by github.com/wbotelhos/raty. It would transfer the "scores" of data attributes to stars.
HTML
<div class="score_show" data-scores="4"></div>
<div class="score_show" data-scores="2"></div>
JS
Method1 failed
$('.score_show').raty({ readOnly: true,score: $(this).data('scores')});
The method failed,so I try another method.
Method2 works
$('.score_show').each(function(){
var score=$(this).data('scores');
$(this).raty({ readOnly: true,score: score});
})
I thought the problem in method1 is that the $(this) did not represent for the correct element.But could someone give me an more detailed explain about the reason method1 failed? Thanks.