1

我试图理解为什么

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

将在外面工作

drop: function(event, ui) {}

jquery 的可放置脚本中的区域,但它不会在里面工作。具体来说,如果我这样做

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

我得到一个Runtime Error (line 1102) data(...).options is null or not an object. $('#title').append('ha');此外,如果我在 drop: 内插入一个,它可以工作。但是,如果我把$('#title').replaceWith('ha');其他地方放在外面

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

有用?

4

2 回答 2

7

我将其发布为答案,但实际上更多的是对 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);
    }
});
于 2011-03-01T02:22:03.623 回答
3

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');
于 2009-07-10T22:06:26.097 回答