我有一个HTML表看起来像这样在一个标准的HTML 4.01过渡页:如何循环表格元素并使用JQuery检索数据元素?
<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()的变体,但一直未能检索“数据”元素。
我已经试过(A $(文件)。就绪()内块...):
$('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 +。
(编辑轻微语法变化)
实际问题就属于这种情况的问题,请参见下面的评论。问题解决了。
我发现我的答案,在我的数据元素中使用了错误的情况。在我的html中,它是data-Element,我不知道它是浏览器,javascript还是jquery,但是data-Element被转换成数据元素,所以当我调用var时,我命名为.data( '元素')有那个名字做数据元素。感谢所有的输入,我喜欢我见过的代码。但在这种情况下,这是一个简单的案例错误。 – user1295718 2012-03-29 13:56:50