嘿,我这里有以下 jquery 代码:
$('th[id*="headingData_"]').each(function () {
fieldNames += $(this).data("dbname") + '~';
console.log(fieldNames);
})
这是我从中提取数据的 HTML:
<tr role="row">
<th id="headingData_0" data-dbname="F_FName" class="sorting_asc" tabindex="0"
aria-controls="theDataTable" rowspan="1" colspan="1" aria-sort="ascending"
aria-label="First Name: activate to sort column ascending"
style="width: 557px;">First Name
</th>
<th id="headingData_1" data-dbname="F_LName" class="sorting" tabindex="0"
aria-controls="theDataTable" rowspan="1" colspan="1"
aria-label="Last Name: activate to sort column ascending"
style="width: 557px;">Last Name
</th>
<th class="sorting" tabindex="0" aria-controls="theDataTable" rowspan="1"
colspan="1" aria-label="Action: activate to sort column ascending"
style="width: 438px;">Action
</th>
</tr>
上面的 jQuery 代码确实有效但是在表格下面还有同名的页脚标签,所以(在这个例子中)有 3 个标题;
名字
姓氏
Action
因此,当我运行它时,它会将 First Name 和 Last Name 加倍。我正在尝试找到一种方法来只获取页眉标签,而不是页脚标签。
页脚 HTML 如下所示:
<tr>
<th id="headingData_0" data-dbname="F_FName" rowspan="1" colspan="1">First Name
</th>
<th id="headingData_1" data-dbname="F_LName" rowspan="1" colspan="1">Last Name
</th>
<th rowspan="1" colspan="1">Action</th>
</tr>
所以我试图找出方法,以便我只查询 tr role="row" 部分以获得这些 header ,仅此而已。在任何给定时间,标题可能有 2 个以上的标题,因此它并不总是只有 2 个标题。
任何帮助都会很棒!
最佳答案
将它添加到选择器的开头:
$('tr[role="row"] > th[id*="headingData_"]').each(function () {
fieldNames += $(this).data("dbname") + '~';
console.log(fieldNames);
});
关于javascript - jQuery 查找 TR->TH 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29608666/