javascript - 使用 DataTable 插件获取具有特定类的表的所有 td 值

标签 javascript jquery html datatable

我在 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);
});

jsFiddle

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

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

关于javascript - 使用 DataTable 插件获取具有特定类的表的所有 td 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740769/

相关文章:

html - DIV 内垂直对齐,div 内有 block 元素

javascript - Selenium,来自浏览器的流式音频

JavaScript:在数组数量中搜索用户输入

javascript:一次预加载一个图像

javascript - 在 Javascript 中从 and OR 回退到三元运算符时出现意外结果

javascript - Sencha Touch 未捕获类型错误 : undefined is not a function

javascript - jquery如何获取文件扩展名

javascript - 自动调整文本区域大小添加了太多额外行

javascript - Jquery 隐藏/显示加载更多按钮

javascript - 加上删除字符串的其他部分并只保留匹配