ReplaceWith 和 jQuery 可拖放?
我试图理解为什么
$('#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将此作为答案发布,但实际上它更多的是对 Jon Erickson 答案的评论(我还没有评论的声誉点)。 18 个月后,这仍然是 IE 中的一个错误,我只是想通过建议 setTimeout() 来详细说明“如何在 drop 函数之外运行某些内容”部分,
我通过传递一个删除元素的匿名函数来解决这个问题setTimeout()。 根据您的捕捉或恢复设置,您可能还需要考虑隐藏可拖动项。
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.
id='title' 的元素是否也有 class='droppable'
我可以看看它是否试图删除一个会导致 drop 事件发生的元素,可能没有更多的元素可以使用,你可能会得到一个 '不是对象的错误。 如果没有亲自尝试的话,我不确定。
也许你可以做的是用一些虚拟类标记对象(jQuery的数据会更合适并且符合SRP,但这超出了这个答案的范围),然后在droppable的drop函数之外你可以做更换
类似...
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...