javascript - 如何根据td选择tr

标签 javascript jquery html arrays

我试图根据“td”类选择“tr”数组,一旦获得数组,我想获取所有 tr 的自定义“data-fid”值的数组。

这就是我得到的,

var rows = $("#tableB tr").children(".New.selected") // returns  td, but I want tr

一旦我得到行,我想获取其中的 data-fid 值的数组,

var selectedValues = rows.attr('data-fid');  // not sure if it will work but i guess this is how i would do it

我正在尝试用谷歌搜索,但我找到正确措辞的运气现在却严重失败。

<table id="tableB" class="dmctable2">
    <tr data-fid="1" Class="selected"><td></td><td></td><td class="New selected"></td></tr>
    <tr data-fid="2"></tr>
    <tr data-fid="3" Class="selected"></tr>
</table>

最佳答案

您可以使用:has()选择器。

Selects elements which contain at least one element that matches the specified selector.

像这样:

$("#tableB tr:has(.New.selected)")

并获取数组中的数据值:

 $("#tableB tr:has(.New.selected)").map(function(){return $(this).data("fid");}).get();

另一个替代方案是 faster将选择 .New.selected 元素,然后访问其 parent :

$("#tableB tr .New.selected").parent('tr')

关于javascript - 如何根据td选择tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25427471/

相关文章:

asp.net - 如何切换脚本标签的可见性?

javascript - 使用 jquery 和数组添加选择选项

javascript - 如何从 URL 中提取 JSON?

javascript - 为什么当我用 JavaScript 覆盖第一个函数时它不起作用

javascript - Angular2 当我刷新页面时,页面不会加载到路由器参数中

jquery - 是否有复选框更改状态的事件?

javascript - knockout : Update to Text box value is not updating the observable item

html - 关于定位css创建的对话泡泡

html - 如何修复崩溃的嵌入式YouTube视频?

javascript - 将 js 对象映射到 V8 中的结构