无为清净楼资源网 Design By www.qnjia.com

看到一个不错的jquery插件,可拖动DIV,顺序可保存到数据库的一个实例:这里就以其中PHP实例简单说明一下:

复制代码 代码如下:
<?php 
//post到后台的数据 
    if ($_POST) { 
        $ids = $_POST["ids"]; 
        for ($idx = 0; $idx < count($ids); $idx+=1) { 
            $id = $ids[$idx]; 
            $ordinal = $idx; 
            //... 
        } 
        return; 
    } 
?> 

复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <style type="text/css"> 
        body { font-family:Arial; font-size:12pt; padding:20px; width: 800px; margin:20px auto; border:solid 1px black; } 
        h1 { font-size:16pt; } 
        h2 { font-size:13pt; } 
        ul { width:350px; list-style-type: none; margin:0px; padding:0px; } 
        li { float:left; padding:5px; width:100px; height:100px; } 
        li div { width:90px; height:50px; border:solid 1px black; background-color:#E0E0E0; text-align:center; padding-top:40px; } 
        .placeHolder div { background-color:white!important; border:dashed 1px gray !important; } 
    </style> 
</head> 
<body> 
    <div> 
        <script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.min.js">
        <h1>jQuery List DragSort PHP Example</h1> 

        <a href="https://www.jb51.net/">Homepage</a><br/> 
        <br/> 

        <h2>Save list order with ajax:</h2> 

        <ul id="gallery"> 
            <?php 
                $list = array("blue", "orange", "brown", "red", "yellow", "green", "black", "white", "purple"); 
                for ($idx = 0; $idx < count($list); $idx+=1) { 
                    echo "<li data-itemid='" . $idx . "'>"; 
                    echo "<div>" . $list[$idx] . "</div>"; 
                    echo "</li>"; 
                } 
            ?> 
        </ul> 

        <script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.dragsort-0.5.1.min.js">        <script type="text/javascript"> 
//saveOrder为回调函数 
            $("#gallery").dragsort({ dragSelector: "div", dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" }); 

            function saveOrder() { 
                var data = $("#gallery li").map(function() { return $(this).data("itemid"); }).get(); 
            //通过ajax模拟post的方式,post格式形式为:[0, 1, 2, 5, 4, 3, 8, 6, 7]  
                $.post("example.php", { "ids[]": data }); 
            }; 
        </script> 

        <div style="clear:both;"></div> 
    </div> 
</body> 
</html> 

标签:
jquery,DIV,可拖动

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com