등급 시스템 인이 코드를 작성했습니다. 내가 원하는 것은 스타 위로 마우스를 가져 가면 트리거되기 전에 별이 나타납니다.jQuery toArray 함수 및 요소 사용 방법
나는 별 위로 마우스를 가져갈 때마다 그림이 바뀌지 만 그 전에는 별이 변하지 않습니다.
$('.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>
UPDATE I는 루프 내부 CONSOLE 넣으면 I 무엇을 얻을이다
<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>
를 왜 I 그것이 TEH 켜져 있음을 알 수 콘솔에 있지만 문서에는 없습니까?
저는 정말로 중요하지 않다고 생각합니다. –
별이있는 컨테이너가 아닌 별의 자녀를 선택하면 논리 오류가 발생합니다. 내 대답을 업데이트했습니다 – Sindre
예, 그렇습니다. '.children()'은 단순히 소스 코드에서 나타나는 요소가 아닌 DOM에서 대상 요소 아래에 계층 적으로있는 요소를 찾습니다. 마우스로 가리키는 별 요소에는 자녀가 없습니다. 그러나 형제가 있기 때문에 $ (this) .parent(). children ('. star')' – jackwanders