繁体   English   中英

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

[英]how to handle .nextAll() or .prevAll() - each element separately?

我正在寻找使我能够分别控制.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
     });
});

谢谢您的帮助

$ .each允许您使用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/

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);
});

你在找那个吗

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

     });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM