評価システムであるこのコードを書きました。私が実現したいのは、星の上にカーソルを置いたときに、その前の星がトリガーされるようにすることです。
星にカーソルを合わせるたびに画像が変わりますが、その前の星は変わりません。
$('.star').hover(function(){
$(this).children().attr('src','StarsRating/star_yellow.png');
var count=$(this).attr('data-count');
var starArray =$('.star').toArray();
for(i=0; i<count; i++){
//The problem is here. the attributes of the stars should change to star_yellow.png..but they dont
console.log(starArray[i]); $(starArray[i]).attr('src','StarsRating/star_yellow.png'); }
},function(){
$(this).children().attr('src','StarsRating/star_grey.png');
});
HTML:
<div id="ratingStars">
<div class="star" data-count="1">
<img src="StarsRating/star_yellow.png"/>
</div>
<div class="star" data-count="2">
<img src="StarsRating/star_grey.png"/>
</div>
<div class="star" data-count="3">
<img src="StarsRating/star_grey.png"/>
</div>
<div class="star" data-count="4">
<img src="StarsRating/star_grey.png"/>
</div>
<div class="star" data-count="5">
<img src="StarsRating/star_grey.png"/>
</div>
コンソールをループ内に配置すると、次のように更新されます。
<div class="star" data-count="1" src="StarsRating/star_yellow.png">…</div>
newEmptyPHPWebPage.php:41
<div class="star" data-count="2" src="StarsRating/star_yellow.png">…</div>
newEmptyPHPWebPage.php:41
<div class="star" data-count="3" src="StarsRating/star_yellow.png">…</div>
しかし、コンソールでは切り替えられているのに、ドキュメントでは切り替えられていないのはなぜですか?