开发者

Jquery: selecting all the elements, appearing after a certain element later on the page

How can I select all the ele开发者_JAVA百科ments, appearing after a certain element, i.e. later on the page


If the elements you are talking of are all siblings to one another, then .nextAll() is the function to use.

If the elements can appear anywhere on the page (no matter what level they are nested at), then you can take advantage of the fact that jQuery (as of version 1.4 onwards) always returns elements in document order, even when you .add() something to the collection.

Therefore the basic idea is to select all the elements first, regardless of whether they are before or after the "start" element, and then add the "start" element to the collection. You can then loop through the sorted elements, looking for the position of the "start" element. Then simply .slice() all elements after this position:

var start = '#start';
var all = 'p';
var index;

var afters = $(all).add(start).each(function (i) {
    if ($(this).is(start)) {
        index = i;
        return false; // quit looping early
    }
}).slice(index + 1);

See it in action here.


Can you provide piece of code or at least a sample of structure or description of what you want to achieve? Have you checked the hierarchy jQuery selectors?


I don't believe there is a simple way to do it. The real question is why would you want to? Anyways, if you really need to, some combination of .nextAll(), .each(), and .children() will help.

var nodes = [];

$('#original').nextAll().each(function() {
  nodes.push(this);
  $(this).children().each(function() {
     // some recursive function to add a deep set of children
  }
}

But, again, you should probably do something different.


How you are identifying your 'certain element'? is it using a specific id. or just a tag name.

You can also add an id to each tag you name as 'certain element' and use nextall() to show all contents after the id. this is just a wild guess.


refer this

another way of doing this

$('selector').nextALL('selector')

refer this nextAll

$('div.third-item').nextAll("div")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜