【问题标题】:jQuery finding TR->TH value(s)jQuery 查找 TR->TH 值
【发布时间】:2015-04-13 15:03:31
【问题描述】:

大家好,我这里有以下 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 个页眉;

名字

动作

所以当我运行它时,它会将 First NameLast 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" 部分以获得这些标题,仅此而已。标题在任何给定时间可能有超过 2 个标题,因此它并不总是只有 2 个标题。

任何帮助都会很棒!

【问题讨论】:

  • 附注,ID必须是唯一的。
  • @j08691 是的,没错,但我使用的 jQuery 插件正在填充您在上面看到的页眉和页脚的 HTML 代码。
  • 在这种情况下,我建议不要使用该插件,因为它显然写得不好。

标签: javascript jquery html wildcard


【解决方案1】:

将其添加到选择器的开头:

$('tr[role="row"] > th[id*="headingData_"]').each(function () {
    fieldNames += $(this).data("dbname") + '~';
    console.log(fieldNames);
});

【讨论】:

  • 做到了。谢谢,乔!
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多