我在 DataTable 上找到了这段代码页面。
var table = $('#example').DataTable();
table.column(0).data().each(function(value, index) {
console.log('Data in index: ' + index + ' is: ' + value);
});
此代码显示第一列所有 td 的所有值。但是,在我的表中,td 可以有不同的类。因此,我只想获取 td 的值,例如,“a”类。
<td class="a"> Hello </td>
<td class="a"> Hi </td>
<td class="b"> By</td>
<td class="b"> How are you?</td>
<td class="a"> Hello world! </td>
代码将返回Hello, Hi, Hello world!
这可能吗?
更新:
我的代码:
<table id="table">
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
</thead>
<tbody>
<tr>
<td class="b">Hello</td>
<td class="b">World</td>
<td class="a">346.387,81</td>
</tr>
<tr>
<td class="b">Hello</td>
<td class="b">World</td>
<td class="a">444.392,35</td>
</tr>
</tbody>
</table>
最佳答案
您可以使用 cells()
基于类名过滤的API:
var table = $('#example').DataTable();
table.cells('.a').data().each(function(value, index) {
console.log('Data in index: ' + index + ' is: ' + value);
});
如果你想限制它到某个列索引,你可以将它作为第二个参数传递给 cells()
,例如:
table.cells('.a', 2).data().each(...);
关于javascript - 使用 DataTable 插件获取具有特定类的表的所有 td 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740769/