gpt4 book ai didi

javascript - 如何循环遍历表元素并使用 JQuery 检索数据元素?

转载 作者:行者123 更新时间:2023-11-30 10:43:10 25 4
gpt4 key购买 nike

我有一个 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'));
});

http://jsfiddle.net/LBKvm/

(但这可能不是最有效的方法)

关于javascript - 如何循环遍历表元素并使用 JQuery 检索数据元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com