jQuery 基于属性创建嵌套表
因此,我昨天问了一个关于嵌套列表的非常类似的问题,认为我可以使用答案来创建嵌套列表和嵌套表。但是当我尝试修改 jQuery 以创建嵌套表时,它变得有点混乱。它要么不嵌套,要么嵌套整个表,其中子 位于父
下,而不仅仅是子
的。示例表:(
<table>
<tbody>
<tr id="10"><td>Parent 1</td></tr>
<tr id="14"><td>Parent 2</td></tr>
</tbody>
</table>
<table>
<tbody>
<tr class="10"><td>Child A</td></tr>
<tr class="10"><td>Child B</td></tr>
<tr Class="14"><td>Child X</td></tr>
</tbody>
</table>
这个 jQuery 基于 vzwick 非常慷慨地帮助我的代码)jQuery 看起来像这样:
$('tbody.csTR_children tr').each(function() {
probable_parent = $('tbody.csTR_parent tr#' + $(this).attr('class'));
if (probable_parent.length) {
if (!probable_parent.find('tbody').length) probable_parent.append('<tbody/>');
$(this).detach().appendTo(probable_parent.find('tbody'));
}
});
这是我得到的最接近的,它在每个父行下嵌套整个表(在 IE 中,它嵌套第一个父行和子行)正确,但其余部分则不然),正如我上面所说。有什么建议吗?
So I asked a very similar question yesterday about nested lists thinking that I would be able to use the answer both for creating nested lists and nested tables. But when I try to modify the jQuery to created nested tables it goes a little haywire. Either it doesn't nest or it nests an entire table with the child <tr>
's under the parent <tr>
instead of just the child <tr>
's. Sample tables:
<table>
<tbody>
<tr id="10"><td>Parent 1</td></tr>
<tr id="14"><td>Parent 2</td></tr>
</tbody>
</table>
<table>
<tbody>
<tr class="10"><td>Child A</td></tr>
<tr class="10"><td>Child B</td></tr>
<tr Class="14"><td>Child X</td></tr>
</tbody>
</table>
(This jQuery is based on code that vzwick very graciously helped me with) The jQuery looks like this:
$('tbody.csTR_children tr').each(function() {
probable_parent = $('tbody.csTR_parent tr#' + $(this).attr('class'));
if (probable_parent.length) {
if (!probable_parent.find('tbody').length) probable_parent.append('<tbody/>');
$(this).detach().appendTo(probable_parent.find('tbody'));
}
});
This is the closest I get and it nests an entire table under each parent row (in IE it nests the first parent and child properly but the rest it doesn't), as I said above. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您试图将
tbody
附加到tr
,这是完全错误的。tbody
必须直接位于table
中,并且您不能将该表直接放入tr
中,它必须位于>td
http://jsfiddle.net/ZDUQU/
所以基本上,将一个表附加到行中的第一个
td
:You are trying to append a
tbody
to atr
which is just completely wrong. Atbody
must be directly within atable
and you can't put that table directly in thetr
, it has to be in atd
http://jsfiddle.net/ZDUQU/
So basically, append a table to the first
td
in the row:看看这个。我认为它可以满足您的需求:
这是一个 jsFiddle 供您使用: http://jsfiddle.net/ 78aXx/2/
鲍勃
Take a look at this. I think it does what you want:
Here is a jsFiddle for you to play with: http://jsfiddle.net/78aXx/2/
Bob