gpt4 book ai didi

javascript - 将 td 置于表格顶部

转载 作者:行者123 更新时间:2023-11-28 18:56:04 26 4
gpt4 key购买 nike

如何将 td 置于表格顶部?我有这样的东西:

    <table>
<tr><td>...</td></tr>
<tr>
<td id="pp" class="text-right"><!-- I want this td in the top of table when a is clicked-->
<a ...>
<i class=""></i>
</a>
</td>
</tr>
<tr><td>...</td></tr>
</table>

最佳答案

一种方法是使用 jQuery 的 prepend() 移动所需的<tr> (行)到表格顶部。
请注意:

If a single element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned).

If there is more than one target element, however, cloned copies of the inserted element will be created for each target except for the last one.

在我的示例中,我使用 closest() 从点击的<td>开始遍历其包含 <tr>这样整行都可以移动。

jQuery('#pp').on('click', function() {
jQuery(this).closest('tr').prependTo('table');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><td>...</td></tr>
<tr><td>...</td></tr>
<tr><td id="pp"><a><i class="">click to move this row to the top</i></a></td></tr>
<tr><td>...</td></tr>
</table>

关于javascript - 将 td 置于表格顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33620961/

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