2015-07-01 28 views
0

如果有人能夠幫助我解決這個問題,那就太棒了。正在尋找下一個從整個頁面上的特定元素

我有元件,其在不同的容器分隔的列表,例如:

<div id="posts"> 
    <div class="container1"> 
    <div class="post"></div> 
    <div class="post"></div> 
    </div> 
    <div class="container2"> 
    <div class="post"></div> 
    <div class="post"></div> 
    </div> 
</div> 

現在我保存的最後一個元素的位置中的可變

var lastPost = $("#posts").find(".post").last(); 

現在我加載更多的帖子進入dom。

... 
<div class="container3"> 
    <div class="post"></div> 
    <div class="post"></div> 
</div> 
... 

從原來的最後位置(lastPost)我想檢索nextAll()的帖子。由於divs不是彼此的兄弟姐妹,我不能通過nextAll()訪問它們。

這將是真棒,如果我能加入職位

回答

1

你可以得到所有的帖子,該項目由lastPost

var $all = $('#posts .post'); 
var $nextAll = $all.slice($all.index(lastPost) + 1); 
+0

那就是我一直在尋找的!謝謝:) –

+0

https://jsfiddle.net/arunpjohny/dxcqe99t/ –

0

您需要遍歷到父元素,然後用nextAll目標同級父容器後進去$("#posts").find(".post") lastPost的位置。

var nextall= lastPost.parent().nextAll().find('.post') 
0

稱爲索引後得到物品,你可以找到最接近的容器,並選擇下的所有容器,並找到.post它們內部

var all = lastPost.closest('[id^="container"]').nextAll().find('.post'); 
相關問題