将 javascript 函数应用于可拖动副本

发布于 2024-11-02 19:31:36 字数 334 浏览 1 评论 0原文

我想应用名为 copy_contenue 的函数来更改 div 父 id 在我拖动原件后创建的副本上,但我的脚本更改了原件而不是副本我也尝试了 ui.helper 来代替它,但没有任何反应

    $('#model_1').draggable({
    connectToSortable:'#global_div',
    addClasses: false,
    helper:'clone', 
    stop: function(e,ui) {
        $(this).replaceWith(copie_contenue("model_1"));
    }
    })

I want to apply function called copie_contenue that change the div parent id
on the copy I created after I dragged my original,but my script change the original not the copy I also tried ui.helper in the place of this but nothing happen

    $('#model_1').draggable({
    connectToSortable:'#global_div',
    addClasses: false,
    helper:'clone', 
    stop: function(e,ui) {
        $(this).replaceWith(copie_contenue("model_1"));
    }
    })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-11-09 19:31:36

要更改新插入的项目,您应该使用可排序的接收事件。然而,截至今天,jQuery UI (1.8.11) 中有一个已知的限制/缺失功能,因此您无法轻松地从接收事件访问克隆的项目。现有的 ui.item 引用原始元素,而不是副本。

作为解决方法,您可以在拖动开始时向原始项目添加一个特殊的类,您可以在接收事件(在将克隆插入到 DOM 后触发)上搜索可排序的项目。在拖动结束时,您必须确保无论发生什么,文档中的任何元素都不应具有特殊的类集。如果您要复制/克隆,这一点尤其重要,因为可排序的接收事件会在可拖动的停止事件之前触发(我们从原始可拖动的特殊类中删除)。

http://jsfiddle.net/3Gkrf/1/

HTML:

<ul>
    <li id="test" class="dragme">Test</li>
</ul>
<ul class="sortme">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

CSS:

.dragme { border:1px solid silver; width:50px; height:50px; }
.newitem { background-color:yellow; }

javascript:

$(function(){
    $(".sortme").sortable({
        receive: function(){
            $(this).find(".newitem").each(function(){
                $(this).removeClass("newitem");

                // Do your stuff with $(this), which is the newly created cloned item
            });
        }
    });

    $(".dragme").draggable({
        connectToSortable: ".sortme",
        helper: "clone",
        start: function(){
            $(this).addClass("newitem");
        },
        stop: function(){
            $(this).removeClass("newitem");
        }
    });
});

如果你想要每当拖动停止时手动创建另一个实例,这可以在可拖动的停止事件上实现。但是,我认为没有可靠的方法来检测它是否被丢弃在可排序的地方或其他地方。

stop: function(){
   var clone = $(this).clone().appendTo("#wherever");
   // do anything else with the clone variable here
}

您必须手动克隆对象,因为即使您设置助手来克隆它,只要拖动停止,助手就会被销毁。如果将其放在可排序对象上,则最终可能会得到两个新对象,因为可排序对象在接收时会自动克隆。

To change the newly inserted item, you should use the sortable's receive event. However, as of today, there is a known limitation/missing feature in jQuery UI (1.8.11) so that you can't easily get access to the cloned item from the receive event. The existing ui.item references the original element, not the copy.

As a workaround, you can add a special class to the original item when dragging starts for which you can search the sortable's items on the receive event (which fires after the clone has been inserted into the DOM). At the end of the drag you have to make sure that whatever happens, no elements in your document should have the special class set. It's especially important if you're copying/cloning, as the sortable's receive event fires BEFORE the draggable's stop event (where we remove the special class from our original draggable).

http://jsfiddle.net/3Gkrf/1/

HTML:

<ul>
    <li id="test" class="dragme">Test</li>
</ul>
<ul class="sortme">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

CSS:

.dragme { border:1px solid silver; width:50px; height:50px; }
.newitem { background-color:yellow; }

javascript:

$(function(){
    $(".sortme").sortable({
        receive: function(){
            $(this).find(".newitem").each(function(){
                $(this).removeClass("newitem");

                // Do your stuff with $(this), which is the newly created cloned item
            });
        }
    });

    $(".dragme").draggable({
        connectToSortable: ".sortme",
        helper: "clone",
        start: function(){
            $(this).addClass("newitem");
        },
        stop: function(){
            $(this).removeClass("newitem");
        }
    });
});

If you just want to create another instance manually whenever dragging stops, that's possible on the draggable's stop event. However, I don't think there is a reliable way in there to detect wether it was dropped on the sortable or somewhere else.

stop: function(){
   var clone = $(this).clone().appendTo("#wherever");
   // do anything else with the clone variable here
}

You have to manually clone your object, because even though you set the helper to clone it, the helper gets destroyed whenever dragging stops. If it WAS dropped on a sortable, you might end up with two new objects though, as the sortable automatically clones on receive.

以往的大感动 2024-11-09 19:31:36

@DarthJDG:实际上它有效,但我将我的可排序项包含在可拖动脚本中,谢谢

$(function() {

             $('.dragging').draggable
            ({
revert: 'invalid',
connectToSortable:'#global_div',
addClasses: false,
helper:'clone',
drag: function(event,ui)
{

    $('#global_div').sortable
    ({

               receive: function(event, ui)
    {
         $(this).find(".dragging").each(function(){


            // Do your stuff with $(this), which is the newly created cloned item
            }:
    }
    });
}

});

@DarthJDG :actually it worked but i included my sortable in the draggable script thanks

$(function() {

             $('.dragging').draggable
            ({
revert: 'invalid',
connectToSortable:'#global_div',
addClasses: false,
helper:'clone',
drag: function(event,ui)
{

    $('#global_div').sortable
    ({

               receive: function(event, ui)
    {
         $(this).find(".dragging").each(function(){


            // Do your stuff with $(this), which is the newly created cloned item
            }:
    }
    });
}

});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文