Jquery 只买5件
我得到了一个对XML文件的AJAX调用,运行良好。 但我只想要第2-6项,而不是全部。 我该怎么做Jquery 只买5件,jquery,Jquery,我得到了一个对XML文件的AJAX调用,运行良好。 但我只想要第2-6项,而不是全部。 我该怎么做 $(xml).find('item').each(function(){ var toplist_no = $(this).find('no').text(); var toplist_user = $(this).find('user').text(); var toplist_won = $(this).find('wo
$(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>');
});
$(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')。在('+toplist'u no+'('+cno+')'+toplist'u user+'+toplist'u won+'-'+toplist'u loose+'')之后;
});
尝试切片:
$(xml).find('item').slice(2,6).each(...
您可以按如下方式计算迭代次数:
k = 0;
$(xml).find('item').each(function(){
if(k <= 5){
...do your stuff
}else {
return false;//breaks the loop
}
k++;
});
k=0;
$(xml).find('item').each(function(){
如果(k你可以添加一个计数器吗
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++;
});
var计数器=0;
$(xml).find('item').each(function(){
//跳过第一项
//或超过第五个的项目
如果(计数器==0){
计数器+++;//增加计数器
continue;//跳过此语句之后的所有内容
}
如果(计数器>4){
//大于4
//我们可以在这里停止循环
打破
}
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')。在('+toplist'u no+'('+cno+')'+toplist'u user+'+toplist'u won+'-'+toplist'u loose+'')之后;
计数器++;
});
插入.filter(函数(i){return(i>=2)&&(i注意:您不必定义额外的变量,each()中的回调函数将索引作为参数。each(函数(i){…})