javascript - 将 td 置于表格顶部

标签 javascript jquery html-table

如何将 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/

相关文章:

Javascript 将数组插入对象内部

javascript - 通过 flask 将谷歌图表的列表信息正确发送到 JS?

javascript - 从 videoJS 显示视频时长

html - 是否可以使用display :block on td in HTML email,来实现响应式表格设计?

如果在表格单元格内,CSS 悬停更改其他文本/类的样式不起作用

javascript - 滚动到元素 onClick JavaScript

javascript - 将自定义 header 添加到 Aurelia Fetch 请求

php - 获取当前日期值到自己的时间

javascript - 创建 Windows 8 资源管理器模拟 - 鼠标选择问题

javascript - 如果表头 <th> 不存在,则将第一个子 <tr><td> 移动为 <th>