replaceWith和jQuery可拖动放置? [英] replaceWith and jQuery draggable drop?

查看:141
本文介绍了replaceWith和jQuery可拖动放置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图了解原因

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

将在

drop: function(event, ui) {}

jQuery的droppable脚本中的

area,但在内部不起作用.具体来说,如果我这样做

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'); 
     }

我得到一个Runtime Error (line 1102) data(...).options is null or not an object.另外,如果我在drop:内插入$('#title').append('ha');,它也可以工作. 但是,如果我将$('#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({ /* */  });

有效吗?

推荐答案

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

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.

也许您可以做的是用一些伪类标记对象(jQuery的数据更适合并符合SRP,但这不在此答案的范围内),然后在droppable的drop函数之外可以替换

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

类似...

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

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

这篇关于replaceWith和jQuery可拖动放置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆