编辑:这里有一个链接向您展示我的示例代码:http://www.singingeels.com/jqtest/
我有一个非常简单的页面,它引用jquery-1.3.2.js、ui.core.js (最新版本)和ui.draggable.js (也是最新版本)。
我有一个div,我可以很容易地拖动它(当然使用鼠标):
<div id="myDiv">hello</div>然后在JavaScript中:
$("#myDiv").draggable();这是完美的工作。但是,我需要能够模拟一个‘拖放’单独使用代码。我让它大部分都能工作,但问题是触发的事件是占位符事件。
如果你打开"ui.core.js“并滚动到底部..。你会看到这个:
// These are placeholder methods, to be overriden by extending plugin
_mouseStart: function(event) { },
_mouseDrag: function(event) { },
_mouseStop: function(event) { },
_mouseCapture: function(event) { return true; }为什么在我的模拟中没有正确地扩展事件,但是当您用鼠标单击时,它们是吗?-对于如何强制_mouseDrag:属性服从"ui.draggable.js“中的覆盖扩展有什么想法吗?
解决这个问题将是巨大的--我计划稍后展示主要的好处。
谢谢,-Timothy
编辑:这里有一个链接向您展示我的示例代码:http://www.singingeels.com/jqtest/
编辑2:点击上面的链接,查看源.你会看到我想做什么的。这里有一个片段:
$(document).ready(function() {
var myDiv = $("#myDiv");
myDiv.draggable();
// This will set enough properties to simulate valid mouse options.
$.ui.mouse.options = $.ui.mouse.defaults;
var divOffset = myDiv.offset();
// This will simulate clicking down on the div - works mostly.
$.ui.mouse._mouseDown({
target: myDiv,
pageX: divOffset.left,
pageY: divOffset.top,
which: 1,
preventDefault: function() { }
});
});发布于 2011-01-05 15:26:37
有一个在JQuery论坛上有关它的问题。在编写本报告时,这个问题还没有解决,但将来可能会有更多的信息。
编辑:论坛上回答:
我建议您使用模拟插件,这是jQuery UI用于单元测试的拖放方式:
https://github.com/jquery/jquery-ui/blob/master/external/jquery-simulate/jquery.simulate.js
您可以通过查看单元测试来查看使用示例。
https://github.com/jquery/jquery-ui/blob/master/tests/unit/draggable/core.js
https://github.com/jquery/jquery-ui/blob/master/tests/unit/draggable/events.js
多亏了rdworth。
发布于 2015-10-02 22:21:00
我找到了一个效果很好的解决方案。我让drop事件调用一个名为onDragAndDrop()的函数。该函数包含两个参数,draggable jQuery对象和droppable jQuery对象。
$('.my-drop-target').droppable({
drop: function(event, ui) {
onDragAndDrop(ui.draggable, $(event.target));
}
});在我的测试中,我有一个直接调用onDragAndDrop的函数,但确保有鼠标的用户可以执行该操作。
var simulateDragAndDrop = function(draggable, droppable) {
if (!draggable.data('uiDraggable')) {
throw new Error('Tried to drag and drop but the source element is not draggable!');
}
if (!droppable.data('uiDroppable')) {
throw new Error('Tried to drag and drop but the target element is not droppable!');
}
onDragAndDrop(draggable, droppable);
}我发现它是一个很好的,易于使用的单元测试解决方案。我可能最终会用它作为键盘辅助的后盾。
https://stackoverflow.com/questions/1166246
复制相似问题