javascript - jQuery 多类和第 n 个 child

标签 javascript jquery each

我有一个包含多个类的表。

<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/

相关文章:

javascript - 有什么方法可以为带有变量的 DOM 对象设置样式吗?

javascript - 显示加载图标,直到表格加载完毕

css - 从 SASS 列表生成关键帧

javascript - ES6函数返回 `undefined`而不是预期的字符串?

javascript - 动态附加google plus按钮的回调参数

javascript - 如何使用javascript一次性显示该数组的字符串内容?

javascript - 确定 JavaScript 类中的问题范围

jquery - rails/CSS : How do I make a gallery of images of different widths?

javascript - 如何在react js中关注页面加载时的html选择或按钮单击?

sass - Get (value + 1) in @each loop with multiple values