gpt4 book ai didi

jquery - 如何使用 jQuery 拆分表行(在 TD 之间插入 TR)

转载 作者:行者123 更新时间:2023-12-01 01:14:28 27 4
gpt4 key购买 nike

我在使用 jQuery 拆分表格时遇到一些问题。

这是表格:

<table width="100%" id="submenu">
<tr>
<td class="submenu">A</td>
<td class="submenu">B</td>
<td class="submenu">C</td>
<td class="submenu">D</td>
<td class="submenu">E</td>
<td class="submenu">F</td>
<td class="submenu">G</td>
<td class="submenu">H</td>
</tr>
</table>

我想在调用函数后使其看起来像这样:

<table width="100%" id="submenu">
<tr>
<td class="submenu">A</td>
<td class="submenu">B</td>
<td class="submenu">C</td>
<td class="submenu">D</td>
</tr>
<tr>
<td class="submenu">E</td>
<td class="submenu">F</td>
<td class="submenu">G</td>
<td class="submenu">H</td>
</tr>
</table>

我尝试过:

$(function(){
$('.submenu td:eq(3)').after('</tr><tr>');
});

最佳答案

试试这个代码片段:

$(function(){
// # Add a new row after the first one in the table
$('table#submenu tr:first').after('<tr></tr>');

// # Move the four last TDs to this new row
$('table#submenu tr:first td.submenu:gt(3)') // Select the four last TDs
.detach() // Detach them from their current row
.appendTo('table#submenu tr:nth-child(2)'); // Add them at the end of the new row
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table width="100%" id="submenu" border="1">
<tr>
<td class="submenu">A</td>
<td class="submenu">B</td>
<td class="submenu">C</td>
<td class="submenu">D</td>
<td class="submenu">E</td>
<td class="submenu">F</td>
<td class="submenu">G</td>
<td class="submenu">H</td>
</tr>
</table>

关于jquery - 如何使用 jQuery 拆分表行(在 TD 之间插入 TR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28300258/

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