jquery - Emulate drag-and-drop using JavaScript - Stack Overflow

I know that jQuery can programmatically trigger events on DOM elements that are listening to those even

I know that jQuery can programmatically trigger events on DOM elements that are listening to those events. For example $el.click() will trigger a click event on $el without having to physically click $el with the mouse.

Given two DOM elements dom1 and dom2, is it possible to programmatically emulate a drag-and-drop from dom1 to dom2 using jQuery (or vanilla JavaScript), where dom1 is draggable and dom2 is droppable using jQuery UI.

Note: The reason I want to do this is to build automated UI tests.

I know that jQuery can programmatically trigger events on DOM elements that are listening to those events. For example $el.click() will trigger a click event on $el without having to physically click $el with the mouse.

Given two DOM elements dom1 and dom2, is it possible to programmatically emulate a drag-and-drop from dom1 to dom2 using jQuery (or vanilla JavaScript), where dom1 is draggable and dom2 is droppable using jQuery UI.

Note: The reason I want to do this is to build automated UI tests.

Share Improve this question edited Jan 20, 2012 at 19:51 Randomblue asked Jan 20, 2012 at 10:13 RandomblueRandomblue 116k150 gold badges363 silver badges558 bronze badges 2
  • "trigger drag-and-drop" is a little vague. I take it you want to create a drag and drop style interface, but do you mean "I want to use the [non existent] builtin drag and drop events"? – Bojangles Commented Jan 20, 2012 at 10:15
  • @JamWaffles: I have edited the question. I have elements which are draggable and droppable. I want to trigger dragging and dropping on those element without using the mouse, just by writing JavaScript code. – Randomblue Commented Jan 20, 2012 at 10:23
Add a ment  | 

4 Answers 4

Reset to default 2

You have to remember that jQuery.trigger does not fire the HTML event, that is, calling .click will not cause a link to be followed. It only triggers events set with jQuery.

http://jupiterjs./news/syn-a-standalone-synthetic-event-library is a library that truly mimics HTML events. It's very easy to use

The following asserts that 'hello' has been removed from the page:

Syn.click( {},'hello' )
   .type( 'Hello World' )
   .delay()         //waits 600ms seconds by default
   .drag( $('#trash') , function() {
     ok( $('#hello').length == 0, "removed hello" )
 }); 

If you're using UI then there are events dragstart, drag, and dragstop on the draggable behavior. You should be able to invoke these programmatically by using jQuery.trigger() just like any other event. But, you'd need to manually manipulate the event instance somehow, to input the coordinates for the item to be dropped at. I'm not sure how well that would work, or why you would want to do it.

You want , with animation move to a new parent. Check the function in JQuery - animate moving DOM element to new parent?

I normally use jquery.ui for plex things with dragging-dropping: http://jqueryui./demos/draggable/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744780738a4593323.html

相关推荐

  • jquery - Emulate drag-and-drop using JavaScript - Stack Overflow

    I know that jQuery can programmatically trigger events on DOM elements that are listening to those even

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信