Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery,.每个函数问题_Jquery - Fatal编程技术网

jQuery,.每个函数问题

jQuery,.每个函数问题,jquery,Jquery,我有以下jQuery。每个循环: $('.table-data tbody tr td').each( function(index) { $('.table-data tbody tr td' + ':nth-child(' + index + ')' ).addClass( 'nth-' + index ); }); 问题是它可以工作,但是.table data tbody tr td'的最后一个元素不会得到类。为什么? 这是因为索引是基于0的吗?如何使它在索引中循环1次?第

我有以下jQuery
。每个循环

$('.table-data tbody tr td').each( function(index) {

    $('.table-data tbody tr td' + ':nth-child(' + index + ')' ).addClass( 'nth-' + index );


});
问题是它可以工作,但是
.table data tbody tr td'
的最后一个元素不会得到类。为什么?


这是因为
索引
是基于0的吗?如何使它在
索引中循环1次?

第n个子项从1开始,因此是的,从零开始的索引将无法在其上正常工作。在使用索引变量的地方,您需要执行index+1,但它将迭代正确的次数。

您不需要使用索引深入到当前元素,这比这更简单

$(".table-data tbody td").each(function(index){
    $(this).addClass("nth-" + index);
});
要对每行中的TD元素重新编号,请尝试以下操作

$(".table-data tbody tr").each(function(index){
    $(this).find("td").each(function(index){
        $(this).addClass("nth-" + index);
    });
});
或者,如果你想更聪明一点,这个

$(".table-data tbody td").each(function(){
    $(this).addClass("n-" + $(this).index());
});

请在操作中查看此项:

$(this)等于$('.table data tbody tr td'+':第n个child('+index++'))@JapanPro-是的,我花了一秒钟的时间来计算出for循环。
$(this).addClass(“第n-”+++index)在我的示例中将是完美的!非常聪明!谢谢如何使它从每行的
0开始计数
?我必须把它放在double
中吗?每个
?double
。每个
看起来都很简单,但不是最专业的编程方式。谢谢你的帮助!在“each”$(this)中表示当前元素。index()方法返回元素相对于其同级元素的位置——在jsbin示例中,是TR中三个TD元素的集合(请参阅)。