Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 循环遍历dom中的li元素_Javascript_Jquery - Fatal编程技术网

Javascript 循环遍历dom中的li元素

Javascript 循环遍历dom中的li元素,javascript,jquery,Javascript,Jquery,我有李元素 <ul class=""> <li class="current-rating" id="current-rating" style="width: 0px"></li> <li><a href="#" title="1 star out of 5" class="one-star star star-rating stars-1">1</a></li>

我有李元素

<ul class="">

        <li class="current-rating" id="current-rating" style="width: 0px"></li>

        <li><a href="#" title="1 star out of 5" class="one-star star star-rating stars-1">1</a></li>

        <li><a href="#" title="2 star out of 5" class="two-stars star star-rating stars-1">2</a></li>

        <li><a href="#" title="3 star out of 5" class="three-stars star star-rating stars-1">3</a></li>

        <li><a href="#" title="4 star out of 5" class="four-stars star star-rating stars-1">4</a></li>

        <li><a href="#" title="5 star out of 5" class="five-stars star star-rating stars-1">5</a></li>

   </ul>
当我将鼠标悬停在3上时,如何将所有元素放到dom上?当我点击或鼠标移到第三个li上时,我想要三个li。当我把鼠标移到第五位时,我就有了所有的第五位。我不知道如何遍历dom,我尝试使用parent()和parents(),但没有成功

对于评级系统,只需使用
.prevAll().andSelf()
。我能做到


但请注意,如果要保持原始选择器,请结合使用:
.andSelf()
,如:

$('li').hover(function(){
   $(this).prevAll().andSelf().css({background: 'gold'});
},function(){
   $(this).prevAll().andSelf().css({background: '#eee'});
});
我让你来做交换类的把戏,然后点击
魔术。你有你需要的一切

对于评级系统,只需使用
.prevAll().andSelf()
。我能做到


但请注意,如果要保持原始选择器,请结合使用:
.andSelf()
,如:

$('li').hover(function(){
   $(this).prevAll().andSelf().css({background: 'gold'});
},function(){
   $(this).prevAll().andSelf().css({background: '#eee'});
});
我让你来做交换类的把戏,然后点击
魔术。你有你需要的一切