简体   繁体   English

Jquery拖放动态创建的div live

[英]Jquery drag and drop on dynamically created div live

I have 2 DIV area on the page. 我在页面上有2个DIV区域。 The left hand side DIV will show the categorys Such as: 1.Team A 2.Team B 3.Team C . 左侧DIV将显示类别如:1。团队A 2.团队B 3.团队C. . .

<div id="source">
<div class="box" id="A">1.Team A</div>
<div class="box" id="B">2.Team B</div>
<div class="box" id="C">3.Team C</div>
</div>

The right hand side DIV will show the members in customized box layout by clicked the team Such as: Clicked Team A 右侧的DIV将通过点击团队显示成员的自定义框布局,例如:Clicked Team A.

<div id="dest">
<div class="memberbox" id="Amember">Peter</div>
<div class="memberbox" id="Bmember">Chris</div>
<div class="memberbox" id="Cmember">Rick</div>
</div>

The right hand side DIV is dynamically created by ajax. 右侧DIV由ajax动态创建。 I cannot using the draggable and droppable function to drag the memberbox. 我无法使用draggable和droppable函数来拖动成员箱。 How it is work? 它是如何工作的?

$(".memberbox").draggable(
                        {
                            cursor: "move",
                            helper: 'clone',
                            appendTo: 'body',
                            zIndex: '999',
                            cursorAt: {top: 15, left: 15},
                            revert: true,
                            scroll: false,
                            addClasses:true,
                            drag: function(event, ui) {
                                this.style.borderColor = 'red';
                            },
                            stop: function(event, ui) {
                                this.style.borderColor = 'black';
                            }


                        }
                );
                $('#source').droppable({
                    activeClass: "active",
                    hoverClass: "hover",
                    drop: handleDropEvent
                });
                function handleDropEvent(event, ui) {
                      var draggable = ui.draggable;
                      console.log(draggable);
                    $(draggable).appendTo('#source');
                }

You have to call the $(".memberbox").draggable( ... ) function & $('#source').droppable(...) function AFTER you add the dynamic elements. 您必须在添加动态元素调用$(".memberbox").draggable( ... )函数和$('#source').droppable(...)函数。

If you are defining them Before adding the elements, then it wont work. 如果你定义它们在添加元素之前,它就不会工作。

Usually I have makeDraggable() function in which I define $(".memberbox").draggable( ... ) & $('#source').droppable(...) . 通常我有makeDraggable()函数,我在其中定义$(".memberbox").draggable( ... )$('#source').droppable(...) This way, once, the dynamic elments are added, I call the makeDraggable() to initialize drag n drop. 这样,一旦添加动态元素,我调用makeDraggable()来初始化拖放。

Another advantage of this mechanism is that if you had to remove and add another set of elements, even then you can call the same function again and it will initialize drag n drop. 这种机制的另一个优点是,如果你必须删除并添加另一组元素,即使这样你也可以再次调用相同的函数,它将初始化拖放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM