Say I had links with up/down arrows for moving a table row up or down in order. What would be the most straightforward way to move that row up or down one position (using jQuery)?

使用jQuery的内置方法似乎没有任何直接的方法来实现这一点,在使用jQuery Select 行之后,我还没有找到一种方法来移动它.此外,在我的例子中,使行可拖动(我以前用插件做过)不是一个选项.

推荐答案

You could also do something pretty simple with the adjustable up/down..

given your links have a class of up or down you can wire this up in the click handler of the links. This is also under the assumption that the links are within each row of the grid.

$(document).ready(function(){
    $(".up,.down").click(function(){
        var row = $(this).parents("tr:first");
        if ($(this).is(".up")) {
            row.insertBefore(row.prev());
        } else {
            row.insertAfter(row.next());
        }
    });
});

HTML:

<table>
    <tr>
        <td>One</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Two</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Three</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Four</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
    <tr>
        <td>Five</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
        </td>
    </tr>
</table>

Demo - JsFiddle

Jquery相关问答推荐

将div中的下一个2个标题分组

通过 Ajax POST 将按钮值从 HTML 发送到 Flask 时出现空数据

多个 AJAX 调用;获取所有失败的呼叫

加载外部 css 文件,如 jquery 中的脚本,这在 ie 中也兼容

通过 .push() 方法向对象添加项目

在 Javascript 中启用和禁用 Div 及其元素

TypeScript 中是否有this的别名?

使用 jQuery 和 HTML 导出为 CSV

使用 jQuery 发送 JSON 数据

如何从 jQuery 中的父级中 Select 所有子级(任何级别)?

jQuery scrollTop 在 Chrome 中不工作但在 Firefox 中工作

jQuery用另一个类替换一个类

jQuery ajax 在 asp.net mvc 中上传文件

使用 jquery 在 radio 上单击或更改事件

JQuery Ajax Post 导致 500 内部服务器错误

jQuery XML 错误请求的资源上不存在‘Access-Control-Allow-Origin’标头.

使用 jQuery DataTables 时禁用第一列的自动排序

如何使用 jQuery 停止默认链接点击行为

jQuery empty() 与 remove()

JavaScript 中的简单节流阀