How to simulate HTML5 Drag and Drop events in javascript?

前端 未结 2 801
醉酒成梦
醉酒成梦 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 04:49

    You can try this one. It uses native HTML5 events, no jQuery

    https://github.com/Photonios/JS-DragAndDrop-Simulator

    0 讨论(0)
  • 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: [] }}));
    
    0 讨论(0)
提交回复
热议问题