我有一个包含多个类的表。
<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 的 first 文本具有类“一”,如果添加另一个类则不会。
我试过一个经典的例子,但它不起作用:
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]);
});
Working Demo
关于javascript - jQuery 多类和第 n 个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28980988/