1. 程式人生 > >多個 DIV 自動 排序(基於Jquery)

多個 DIV 自動 排序(基於Jquery)

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var div = $('div').toArray().sort(function (a, b) {
                return parseInt($(a).attr("str")) - parseInt($(b).attr("str"))
            });
           $(div).appendTo($("#tabOrders").children().children().children());
            //alert($("#tabOrders").parent().parent().parent().children("td:first").find("div:first").html());
            //$(div).appendTo('$("#tabOrders").parent().parent().children("td : first")')
        });
              

    </script>

<body>
    <table width="100%" cellspacing="0" cellpadding="0" border="0">
        <tbody>
            <tr>
                <td>
                    <table id="tabOrders" width="100%" cellspacing="0" cellpadding="0" border="0">
                        <tbody>
                            <tr>
                                <td>
                                    <div str="11">
                                        11</div>
                                    <div str="33">
                                        33</div>
                                    <div str="55">
                                        55</div>
                                    <div str="22">
                                        22</div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</body>

當然:body裡面的標籤都可以自定義,大家隨機應變就可以啦