2017-08-16 23 views
0

我已經使用索引參數對每個循環進行了計數多個帖子。當Index參數設置爲值時,繼續計數

我想將該索引參數設置爲某個值,然後從那裏計數。

但是當我將它設置爲循環中的一個值時,它不會繼續計數,它只是輸出該循環中對象數的指定值。

我附上了下面的代碼片段和console.log結果的屏幕截圖。

CODE -

$.each(articleArray[0], function(i) { 
 
     i = postID; 
 
     slideCount++; 
 
     console.log(i++); 
 
     articleContent = articleArray[(pageNumber - 1)][i].content; 
 
     $('.swiper-slide:nth-child(' + slideCount + ')').html(articleContent); 
 
     
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Console Screen

+0

'postID'從哪裏來?那是你想要開始的地方嗎? –

+0

是的,所以當您點擊帖子時,postID來自數據值。所以說它剛剛設置爲1,然後將我設置爲1,然後從那裏繼續計數。 – Darian

回答

2

不要使用索引參數。用你自己的變量創建$.each循環:

var counter = postID; 
$.each(articleArray[0], function() { 
    slideCount++; 
    articleContent = articleArray[(pageNumber - 1)][counter++].content; 
    $('.swiper-slide:nth-child(' + slideCount + ')').html(articleContent); 
}); 

注意,我搬到哪裏就可以了增量是,從console.log(我以爲是暫時的)實際使用它。如果你的意思是它開始於postID + 1(這是console.log增加了它),只需將+ 1添加到第一行定義counter

+1

這工作!謝謝,當Stack允許我時,將標記爲正確。我很感激幫助。 – Darian