以编程方式创建拖动事件时,dataTransfer为空 [英] dataTransfer is null when creating drag event programmatically

查看:156
本文介绍了以编程方式创建拖动事件时,dataTransfer为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图创建一个dragEvent并使用Chrome上的代码以编程方式触发它:

  var ev = document.createEvent(MouseEvents); 
ev.initEvent(dragstart);

然后以这种方式启动:

  element.dispatchEvent(EV); 

元素具有draggable属性。



事件已成功发送。但是,在触发事件时,dataTransfer对象为null。即使我尝试将其手动设置为工作的dataTransfer对象 - 它仍然保持为空。




任何想法?


感谢

解决方案

不幸的是,似乎没有这样一种方式此时以编程方式创建DataTransfer对象。 HTML5拖放界面的描述说明:


尽管为了与其他事件接口一致,DragEvent接口有一个构造函数,它并不是特别有用。特别地,没有办法从脚本创建有用的DataTransfer对象,因为DataTransfer对象具有在拖放时由浏览器协调的处理和安全模型


请在此查看更多详情: http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#the-dragevent-and-datatransfer-interfaces



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.

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

解决方案

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:
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.
Please check more details here: http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#the-dragevent-and-datatransfer-interfaces

这篇关于以编程方式创建拖动事件时,dataTransfer为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆