开发者

Droppable div created on the fly not working

I am using class for setting draggable and droppable properties of the element (UL, LI or DIV). So i can drag and drop as intended when i create the elements at design time. But when i create a element on the fly ( A div with id='ontheflyDiv' ) is not working as droppable.

Thanks in advance.

<html xmlns="">
<head>
    <title></title>
    <link rel="stylesheet" href="/Styles/themes/base/jquery.ui.all.css">
    <script src="/Scripts/jquery-1.6.2.js"></script>
    <script src="/Scripts/ui/jquery.ui.core.js"></script>
    <script src="/Scripts/ui/jquery.ui.widget.js"></script>
    <script src="/Scripts/ui/jquery.ui.accordion.js"></script>
    <script src="/Scripts/ui/jquery.ui.mouse.js" type="text/javascript"></script>
    <script src="/Scripts/ui/jquery.ui.draggable.js" type="text/javascript"></script>
    <script src="/Scripts/ui/jquery.ui.droppable.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/Styles/demos.css">
    <script>

        $(function () {

        //Draggable Items
            $(".myDragClass").draggable({
                helper: 'clone',
                appendTo: 'body'
            });
        //Droppable
            $(".myDropClass").droppable(
                {
                    drop: function (event, ui) {
                        //remove old item
                        $(this).find(".myDragClassPlaceHolder").remove();

                        var listHtml = "<li class='myDragClass' style='width: 200px;color:#000000;background-color:#00FF00;'>" +
                                        "<div id='ontheflyDiv' class='myDropClass' style='width:200px;height:100px;'>" +                                        
                                        "New Droppable area" + ui.draggable.text() + "</div></li>";

                        $(this).append(listHtml);
                    }
                });

       });
    </script>
</head>
<body>
    <div id="div3" class="demos-nav">
        <ul class="myDropClass">
            <li id="li7" class="myDragClassPlaceHolder">Drag and drop order here</li>
        </ul>
    </div>
    <BR />
    <div id="Div1" style="float: none; clear: both; font-size: 12px; height: 100px; overflow: scroll;">
        <table>
            <tr>
                <td><B><U>Order No</B></U></td>
            </tr>
            <tr>
                <td>
                    <div class="myDragClass">5000162255</div>
                </td>
            </tr>
            <tr>
                <td>
         开发者_如何学运维           <div class="myDragClass">
                        5000162266</div>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>


I think you need to make your newly appended element droppable by calling droppable() on it after your've added it to the DOM. if you add something like this:

    $(this).find('#ontheflyDiv').droppable( { ... }); 

to the bottom of your drop function it should work. I'm not aware of a way to add droppable functionality to elements added after page load similar to what jquery's live() function can do for click and other events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜