-1

我想使用 jQuery(不使用 jQuery 插件)在 HTML 表中随机拖放一个表行。

你有什么建议吗?

4

1 回答 1

1

先说最简单的。你认为 jquery ui 是一个插件吗?

如果没有,那么您应该寻找它的可排序方法 http://jqueryui.com/demos/sortable/ 他们也有一个可放置和可拖动的方法。

使用 sortable,您可以快速将可排序行为添加到表中。由于您需要能够混合标题行和正文行,您可以尝试 connectWith 选项:

<body>
    <table>
        <tr class="connectedSortable">
            <th>Column1</th>
            <th>Column2</th>
            <th>Column3</th>
            <th>Column4</th>
            <th>Column5</th>
        </tr>
        <tr class="connectedSortable">
            <td>item1</td>
            <td>item2</td>
            <td>item3</td>
            <td>item4</td>
            <td>item5</td>
        </tr>
        <tr class="connectedSortable">
            <td>item11</td>
            <td>item22</td>
            <td>item33</td>
            <td>item44</td>
            <td>item55</td>
        </tr>
    </table>

    <script type="text/javascript">
        (function($){
            $(document).ready(function(){
                $('table tr').sortable({
                    connectWith: ".connectedSortable"
                });
            });
        })(jQuery);
    </script>

</body>
于 2012-08-23T10:01:55.643 回答