How to simulate HTML5 Drag and Drop events in javascript?

前端 未结 2 796
醉酒成梦
醉酒成梦 2021-02-07 04:07

As title, I\'m trying to simulate an HTML5 drag and drop event in javascript.

I\'ve looked into jquery.ui.simulate and also the simulate function here. Both seem to be a

2条回答
  •  执念已碎
    2021-02-07 05:05

    Your best bet is to recreate a fake event object.

    In my unit tests for a drag-and-drop file upload library (Droplit, shameless plug), I have been doing this with jQuery's Event method. In my source code I set my drop event listener with element.ondrop() like so:

    element.ondrop = function(e) {
      e.preventDefault();
      readFiles(e.dataTransfer.files);
    };
    

    And then I can test this out by building a fake event object and passing it into the ondrop method.

    element.ondrop($.Event('drop', {dataTransfer: { files: [] }}));
    

提交回复
热议问题