2014-10-02 2 views
-1

나는이 AJAX 호출을 XML 파일 인 WOrks라고합니다. 그러나 나는 단지 2-6 아이템을 갖고 싶습니다. 어떻게 할 수 있습니까?만 5 항목 가져 오기

$(xml).find('item').each(function(){ 

      var toplist_no = $(this).find('no').text(); 
      var toplist_user = $(this).find('user').text(); 
      var toplist_won = $(this).find('won').text(); 
      var toplist_loose = $(this).find('loose').text(); 

      $('#toplisttable_' + cno + ' tr:last').after('<tr><td>' + toplist_no + '(' + cno + ')</td><td>' + toplist_user + '</td><td>' + toplist_won + '-' + toplist_loose + '</td></tr>'); 

}); 

답변

3

시도 슬라이스 :

k = 0; 

$(xml).find('item').each(function(){ 
    if(k <= 5){ 
     ...do your stuff 
    }else { 
     return false;//breaks the loop 
    } 
    k++; 
}); 
1

당신이 같은 반복을 셀 수?

var counter = 0; 
$(xml).find('item').each(function(){ 

    // skip first item 
    // or items beyond the fifth 

    if (counter == 0) { 
     counter++; // increase counter 
     continue; // skip everything after this statement 
    } 

    if (counter > 4) { 
     // larger then 4 
     // we can stop the loop here 
     break; 
    } 

    var toplist_no = $(this).find('no').text(); 
    var toplist_user = $(this).find('user').text(); 
    var toplist_won = $(this).find('won').text(); 
    var toplist_loose = $(this).find('loose').text(); 

    $('#toplisttable_' + cno + ' tr:last').after('<tr><td>' + toplist_no + '(' + cno + ')</td><td>' + toplist_user + '</td><td>' + toplist_won + '-' + toplist_loose + '</td></tr>'); 

    counter++; 

}); 
0

주 전에 : 각()의 콜백 함수를 매개 변수로 인덱스를 취하면 별도의 변수를 정의 할 필요가 없습니다 .each(function(i){...})

관련 문제