使用 DataTable 插件获取具有特定 class 的 table 的所有 td 值

Get all td values of a table that have a certain class with DataTable plugin

我在 DataTable 页面上找到了这段代码。

var table = $('#example').DataTable();
table.column(0).data().each(function(value, index) {
    console.log('Data in index: ' + index + ' is: ' + value);
});

此代码显示第一列所有 td 的所有值。但是,在我的 table 中,td 可以有不同的 class。因此,我只想获取 td 的值,例如 "a" class.

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

代码会returnHello, 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 根据 class 姓名进行过滤:

var table = $('#example').DataTable();
table.cells('.a').data().each(function(value, index) {
    console.log('Data in index: ' + index + ' is: ' + value);
});

jsFiddle

如果你想将它限制在某个列索引,你可以将它作为第二个参数传递给cells(),例如:

table.cells('.a', 2).data().each(...);