jQuery multiple class and nth-child

jQuery multiple class and nth-child

本文关键字:nth-child and class multiple jQuery      更新时间:2023-09-26

我有一个有多个类的表。

<tr>
<td class='one two'>One</td>
<td class='one three'>Another</td>
<td class='one example'>Yet another</td>
<td class='one two'>Yet another</td>
<td class='four'>Four</td>
</tr>
<tr>
<td class='one two'>One</td>
<td class='one three'>Another</td>
<td class='one example'>Yet another</td>
<td class='one two'>Yet another</td>
<td class='four'>Four</td>
</tr>
<tr>
<td class='one two'>One</td>
<td class='one three'>Another</td>
<td class='one example'>Yet another</td>
<td class='one two'>Yet another</td>
<td class='four'>Four</td>
</tr>

我想要的是设置包含 td 的每个 tr 中的第一个文本具有类"一",如果添加另一个类,则没有马瑟。

我尝试过一个经典的例子,但它不起作用:

        var i=0;
   $(".one:nth-child(1)").each(function() {
    temp[i] = $(this).text;
    console.log("Result: "+temp[i]);
        i++;
     });

但这行不通。

您需要使用:

$("tr td.one:first-child").each(function(i) {
  temp[i] = $(this).text();
  console.log("Result: "+temp[i]);
});

工作演示