使用 jQuery 将内容添加到表中的特定列
我有一个看起来像这样的表结构。一张外面的桌子,几乎每个 td 中我都有一张里面的桌子。
<table class="outertable">
<tr>
<td>
<table class="innertable">
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</td>
<td>
<table class="innertable">
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</td>
</tr>
.....
</table>
使用 jQuery,我需要循环遍历外部表中的每个 td,以在某些特定 td 中添加一些独特的内容。我一直在尝试使用以下代码。如果外表没有任何子表,它可以正常工作,但正如您所理解的,它不适用于子表。那么如何循环遍历表中的每个td来添加内容呢?
var row = 0;
car column = 0;
for (var i = 0; i < list.length; i++) {
$(".outertable tr:eq(" + row + ") td:eq(" + column + ")").append(list[i].content);
if (column == 1) {
column = 0;
row++;
} else {
column++;
}
}
I have a table structure looking something like this. One outer table and in almost each td I got an inner table.
<table class="outertable">
<tr>
<td>
<table class="innertable">
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</td>
<td>
<table class="innertable">
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</td>
</tr>
.....
</table>
With jQuery I need to loop through each td in the outer table to add some unique content in some specific tds. I have been trying to use the following code. It works fine if the outer table doesn't have any child tables but as you can understand it doesn't work with the child tables. So how can I loop through each td in the table to add content?
var row = 0;
car column = 0;
for (var i = 0; i < list.length; i++) {
$(".outertable tr:eq(" + row + ") td:eq(" + column + ")").append(list[i].content);
if (column == 1) {
column = 0;
row++;
} else {
column++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信你的问题是你的 CSS 选择器是贪婪的。 “.outertable tr”将获取“.outertable”下的所有子“tr”元素。相反,调整选择器以仅获取直接子级“.outertable > tr”。选择器的“td”部分也是如此。所以...
I believe your problem is the fact that your CSS selectors are greedy. ".outertable tr" will get all children 'tr' elements under '.outertable'. Instead, adjust the selector to only get direct children ".outertable > tr". Same goes for the 'td' part of the selector. So...