作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 html 表格,在标准 HTML 4.01 过渡页面中看起来像这样:
<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>etc...</th>
</tr>
</thead>
<tbody id="tableBodyID">
<tr class="rowElement" data-element="some-data-here">
<td>Some Table Data</td>
<td>Some More Table data</td>
<td>etc.</td>
</tr>
<tr class="rowElement" data-element="some-data-here">
<td>Some Table Data</td>
<td>Some More Table data</td>
<td>etc.</td>
</tr>
</tbody>
</table>
我想遍历所有行并获取数据元素字段中的“数据”,然后使用 JQuery 将其放入一个变量中。我尝试了 .children()、.data() 和 .each() 的多种变体,但无法检索“数据”元素。
我试过(在 $(document).ready() block 中...):
$('tbody > tr').each(function() {
alert( $(this).data('element')); // CORRECTION - this works.
});
$('tbody').children().each(function() {
alert( $(this).data('element')); // CORRECTION - this works
});
$('.rowElement').each(function(i, obj) {
rowValue = $(this);
alert(rowValue.data('element')); // CORRECTION - this works
});
欢迎任何帮助。我正在使用 HTML 4.01 和 JQuery 1.7.1。我一直在 Firefox 中测试,但需要支持其他标准浏览器 Firefox、Opera、Safari、Chrome 和 IE8+。
(针对较小的语法更改进行了编辑)
实际问题是案例问题,请看下方评论。问题已解决。
最佳答案
这对我有用:
$('table').find('tbody > tr').each(function() {
alert( $(this).data('element'));
});
(但这可能不是最有效的方法)
关于javascript - 如何循环遍历表元素并使用 JQuery 检索数据元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925830/
我是一名优秀的程序员,十分优秀!