如何使用jQuery在HTML表中随机拖放表行?

时间:2012-08-23 09:57:05

标签: jquery html

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

你有什么建议吗?

1 个答案:

答案 0 :(得分: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>