問題描述
我正在嘗試創建一個 dragEvent 并在 Chrome 上使用此代碼以編程方式觸發它:
I am trying to create a dragEvent and fire it programmatically using this code on Chrome:
var ev = document.createEvent("MouseEvents");
ev.initEvent("dragstart");
然后以這種方式觸發它:
and then fire it this way:
element.dispatchEvent(ev);
元素具有可拖動屬性.
The element has draggable attribute.
事件已成功發送.但是,在觸發事件時 - dataTransfer 對象為空.即使我嘗試手動將其設置為工作 dataTransfer 對象 - 它仍然保持為空.
The event was dispatched successfully. However on the event being fired - the dataTransfer object is null. Even when I try to set it manually to a working dataTransfer object - it still stays null.
有什么想法嗎?
謝謝
Any ideas?
Thanks
推薦答案
很遺憾,目前似乎沒有這樣的方法可以通過編程方式創建 DataTransfer 對象.HTML5 拖放界面的描述說:
Unfortunately, it seems that there is no such a way to create a DataTransfer object programmatically at the moment. The description of HTML5 drag and drop interface says:
雖然為了與其他事件接口保持一致,DragEvent 接口有一個構造函數,但并不是特別有用.特別是,無法從腳本創建有用的 DataTransfer 對象,因為 DataTransfer 對象具有在拖放過程中由瀏覽器協調的處理和安全模型
Although, for consistency with other event interfaces, the DragEvent interface has a constructor, it is not particularly useful. In particular, there's no way to create a useful DataTransfer object from script, as DataTransfer objects have a processing and security model that is coordinated by the browser during drag-and-drops
請在此處查看更多詳細信息:http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#the-dragevent-and-datatransfer-interfaces
Please check more details here: http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#the-dragevent-and-datatransfer-interfaces
這篇關于以編程方式創建拖動事件時,dataTransfer 為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!