如何处理.nextAll()或.prevAll() - 每个元素分开?

时间:2011-08-04 11:38:34

标签: jquery

我正在寻找能让我能够分别控制.prevAll()或.nextAll()jQuery方法返回的每个元素的东西。

类似的东西:

<div id="home">
   <div class="box"></div>
   <div class="box"></div>
   <div class="box"></div>
   <div class="box"></div>
   <div class="box"></div>
</div>

等等:

$('#home .box').mouseover(function(){

var hovBox = $(this);
var prevAll = hovBox.prevAll();
var nextAll = hovBox.nextAll();

     nextAll.each(function(){

           // ...and do something with each returned element
           // NOT with ALL returned elements, just handle each separately
           // through some other type of selector
     });
});

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

$。每个允许您使用this

访问DOM元素
nextAll.each(function(index){
    // ...and do something with each returned element
    $(this).fadeTo('fast', index/5);
});

这是小提琴: http://jsfiddle.net/2MMGw/1/

http://api.jquery.com/each/

答案 1 :(得分:2)

nextAll.each(function(index){
   //index=0 will be the first box after that one
   //index=1 will be the 2nd box 
   //index=2 will be the 3nd box 

   alert(index);
});

例如,如果你想要第二个具体的东西

  nextAll.each(function(index){
     //do something
    if (index==1)
    {
    $(this).hide(); //do specific thing
    }
    alert(index);
});

答案 2 :(得分:0)

这是你要找的东西吗

   nextAll.each(function(i){            
       if(i>0){
          $(this).css('color','#ff000');// will give color all elements after 1st element that you hover
        }

     });