javascript - jquery动态表id选择

标签 javascript jquery html

我是 jquery 新手。可能我的查询看起来有些愚蠢,但实际上我不明白如何实现下面的事情。

无论如何,这是我的查询:

我有一些动态表如下:

<table id='example1'>
<thead>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
</thead>
</table>

<table id='example2'>
<thead>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
</thead>
</table>

我正在尝试如下:

$($('table').attr('id') ' thead th').each( function () {

}

上面好像有问题,有人可以纠正吗?

我需要实现如下:

$( dynamicTableId thead th).each(function () {

}

最佳答案

如果您尝试迭代所有第 th 元素,则无需使用,只需使用表作为选择器即可。您还可以使用 has 属性选择器来确保它有一个 id

$('table[id] thead th').each( function () {

})

关于javascript - jquery动态表id选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29795888/

相关文章:

javascript - 如何使用 JavaScript 动态创建表单?

javascript - 我可以在 col-md-8 中使用两个 col-md-6 吗?

javascript - 从列表中选择项目并使用 Javascript AngularJS 将其发送回服务器

javascript - 在简单示例中未检测到滚动事件

Javascript - 揭示对象和变量范围

javascript - Jquery 不处理 href 点击

javascript - 查找当前类是否包含单词,然后将新类附加到这些 li 中 - jquery

javascript - 将动态形式恢复到初始状态

html - 使下一个 div 保持在当前 div 下面

javascript - Uncaught ReferenceError : toggleSidebar is not defined