您可以将方法传递给revert option http://jqueryui.com/demos/droppable/#revert同样,例如,如果我们采用这样的示例演示:
<div id="draggable" class="ui-widget-content">
<p>I revert when I'm not dropped</p>
</div>
<div id="droppable" class="ui-widget-header">
<p>Drop me here</p>
</div>
然后在您的可拖动对象上,您可以计算是否恢复,如下所示:
$("#draggable").draggable({
revert: function(dropped) {
var dropped = dropped && dropped[0].id == "droppable";
if(!dropped) alert("I'm reverting, what a cruel world!");
return !dropped;
}
});
你可以在这里尝试一下 http://jsfiddle.net/nick_craver/bVkwt/, the dropped
传入的参数是它被放置到的元素.droppable()
, 只是false
如果它降落在其他地方。
或者如果您正在使用accept option http://jqueryui.com/demos/droppable/#option-accept你可能想计算一下,如下所示:
var dropped = dropped && $(this).is(dropped.droppable('option', 'accept'));
这使用了接受选择器并使用.is() http://api.jquery.com/is/查看可放置的是否匹配。
您可以在此处查看演示 http://jsfiddle.net/nick_craver/bVkwt/1/.