2012-08-06 33 views
1

我写了这个代码,这是一个评级系统。 我想要发生的事情是,当你将鼠标悬停在一颗恒星上时,星星应该触发它。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这就是我得到的,当我把控制台内部的循环:

<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> 

但为什么我可以看到,它开启TEH控制台,但不在文档上?

回答

0

多数民众赞成答案:

 <script type="text/javascript"> 
     $('.star').hover(function(){ 
      $(this).children().attr('src','StarsRating/star_yellow.png'); 
      var count=$(this).attr('data-count'); 
      **var starArray =$('.star').children().toArray();** 

      for(i=0; i<count; i++){ 

       var current= $(starArray[i]); 
       current.attr('src','StarsRating/star_yellow.png'); 
      } 

     },function(){ 
      $('.star img').attr('src','StarsRating/star_grey.png'); 


     }); 
    </script> 

VAR starArray = $( '星')指定者(); < - 我得到了div

var starArray = $('。star')。children()。toArray(); < - 现在我得到的图像

1

你有一个问题,你是逻辑。在里面你会悬停功能,你会得到一个明星对象的孩子,但你真正想要的是明星的对象:)

的父母的孩子你有:

$(this).children().attr('src','StarsRating/star_yellow.png'); 

下工作:

$(this).parent().children().attr('src', 'StarsRating/star_yellow.png'); 

由于从jackwanders评价: 。儿童()发现,在层次在DOM中目标元件的下方,而不是简单后中出现的源代码的那些元件的元件。你盘旋的明星元素没有孩子。然而,它有兄弟姐妹,所以$(this).siblings('。star')会起作用,$(this).parent()。children('。star')

而且这也必须进行更改:

var starArray = $('.star').toArray(); 

$('.star').children().toArray(); 

也;而不是:如果你想强调所有的星星直至并包括恒星正在上空盘旋,数组和循环不是http://api.jquery.com/each/

$("#ratingStars").each(function(index) { 

    if(index >= count) 
     return false; // break 

    $(this).attr('src', 'StarsRating/star_yellow.png'); 


}); 
+0

我真的,不认为它很重要 – 2012-08-06 18:20:54

+0

你的代码有一个逻辑错误,因为你选择了一个明星的孩子,而不是星星的容器。我已经更新了我的回答 – Sindre 2012-08-06 18:27:03

+1

是的,它的确如此。 '.children()'发现层次低于DOM中目标元素的元素,而不仅仅是那些出现在源代码中的元素。你盘旋的明星元素没有孩子。它有兄弟姐妹,所以'$(this).siblings('。star')'会起作用,就像$(this).parent()。children('。star')' – jackwanders 2012-08-06 18:27:46

2

 for(i=0; i<count; i++){ 
      //The problem is here. the attributes of the stars should change to star_yellow.png..but they dont 
      $(starArray[i]).attr('src','StarsRating/star_yellow.png'); 
     } 

尝试jQuery的。每个功能如果您使用.prevAll功能,则必须使用该功能。

尝试:

$('.star').hover(function() { 
    var star = $(this); 
    star.add(star.prevAll('.star')).find('img').attr('src','StarsRating/star_yellow.png'); 
},function() { 
    $(this).parent().children('.star').find('img').attr('src','StarsRating/star_grey.png'); 
}); 

第一个函数查找悬停明星之前的所有兄弟姐妹,并把它们(以及悬停星)黄色。第二个函数查找容器元素的所有子星,并将它们变成灰色。

+0

看答案我发贴 – 2012-08-06 18:36:17

+0

是吗?你是指设置图像的'src'而不是div容器?我的解决方案也是一样的('.find('img')')。它还避免了创建for循环的需求,因为您试图实现的功能由'.prevAll'函数处理得很好。 – jackwanders 2012-08-06 18:39:06