ReplaceWith 和 jQuery 可拖放?

发布于 2024-07-26 15:54:40 字数 612 浏览 10 评论 0原文

我试图理解为什么

$('#title').replaceWith('ha'); 

在 jquery 的可放置脚本中可以在该区域之外工作

drop: function(event, ui) {}

,但在内部却无法工作。 具体来说,如果这样做,

$(".droppable").droppable({
drop: function(event, ui) {
    $('#title').replaceWith('ha'); 
     }

我会收到运行时错误(第 1102 行)data(...).options 为 null 或不是对象。 另外,如果我在 drop: 内插入 $('#title').append('ha'); ,它就会起作用。 但是,如果我将 $('#title').replaceWith('ha'); 放在它之外的任何其他地方,

$(".droppable").droppable({ /* */  });

它会起作用吗?

I'm trying to understand why

$('#title').replaceWith('ha'); 

will work outside the

drop: function(event, ui) {}

area in jquery's droppable script, but it won't work inside. Specifically, if I do

$(".droppable").droppable({
drop: function(event, ui) {
    $('#title').replaceWith('ha'); 
     }

I get a Runtime Error (line 1102) data(...).options is null or not an object. Also if I insert a $('#title').append('ha'); inside the drop:, it works.
However if I put $('#title').replaceWith('ha'); anywhere else outside

$(".droppable").droppable({ /* */  });

it works?

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

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

发布评论

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

评论(2

一江春梦 2024-08-02 15:54:40

我将此作为答案发布,但实际上它更多的是对 Jon Erickson 答案的评论(我还没有评论的声誉点)。 18 个月后,这仍然是 IE 中的一个错误,我只是想通过建议 setTimeout() 来详细说明“如何在 drop 函数之外运行某些内容”部分,

我通过传递一个删除元素的匿名函数来解决这个问题setTimeout()。 根据您的捕捉或恢复设置,您可能还需要考虑隐藏可拖动项。

$(".droppable").droppable({
    drop: function(event, ui) {
        // do something interesting here...

        // now get rid of the draggable
        $(ui.draggable).hide();           
        setTimeout(function(){$(ui.draggable).remove();}, 1);
    }
});

I'm posting this as an answer but really its more of a comment on Jon Erickson's answer (I don't have the reputation points to comment yet). 18 months later this is still a bug in IE and I just wanted to elaborate on the 'how to run something outside of the drop function' part by suggesting setTimeout()

I am solving the problem by passing an anonymous function that removes the element to setTimeout(). Depending on your snap or reversion settings you might want to also consider hiding the draggable.

$(".droppable").droppable({
    drop: function(event, ui) {
        // do something interesting here...

        // now get rid of the draggable
        $(ui.draggable).hide();           
        setTimeout(function(){$(ui.draggable).remove();}, 1);
    }
});
苄①跕圉湢 2024-08-02 15:54:40

id='title' 的元素是否也有 class='droppable'

我可以看看它是否试图删除一个会导致 drop 事件发生的元素,可能没有更多的元素可以使用,你可能会得到一个 '不是对象的错误。 如果没有亲自尝试的话,我不确定。

也许你可以做的是用一些虚拟类标记对象(jQuery的数据会更合适并且符合SRP,但这超出了这个答案的范围),然后在droppable的drop函数之外你可以做更换

类似...

$(".droppable").droppable({
    drop: function(event, ui) {
        // mark the element for replacement
        $('#title').addClass('replaceThisElement'); 
    }
});

// outside of the drop function
$('#title .removeThisElement').replaceWith('ha');

Does the element with id='title' also have class='droppable'

I could see if it is trying to remove an element that would cause the drop event to occur there may be no more element to work with and you could get a 'not an object' error. I don't know for sure without trying this out myself.

Maybe what you can do is mark the object with some dummy class (jQuery's data would be more suitable and comply with the SRP, but that is out of the scope of this answer), and then outside of the droppable's drop function you can do the replacement

something like...

$(".droppable").droppable({
    drop: function(event, ui) {
        // mark the element for replacement
        $('#title').addClass('replaceThisElement'); 
    }
});

// outside of the drop function
$('#title .removeThisElement').replaceWith('ha');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文