简体   繁体   English

HTML5 中的拖放操作

[英]Drag and Drop operation in HTML5

Drag and Drop operation requires to set up a function that specifies what data is to be dragged, for instance:拖放操作需要设置一个函数来指定要拖放哪些数据,例如:

HTML code: HTML代码:

<img id="drag1" src="img_logo.gif" draggable="true" ondragstart="drag_start(event)" width="336" height="69">

Javascript code: Javascript代码:

function drag_start(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

It's not to clear to me if the first parameter of the setData method should be "text" or "text/plan" (or something else).我不清楚 setData 方法的第一个参数应该是"text"还是"text/plan" (或其他东西)。

The example here uses "text" , but the example here uses "text/plain" and other types.这里的例子使用了"text" ,但这里的例子使用了"text/plain"和其他类型。

It can be whatever you want.它可以是任何你想要的。 For example, in my code, I have例如,在我的代码中,我有

domItem.ondragstart = event => {
            event.dataTransfer.setData("itemid", this.id);
        };

and then, on the drag target, I have然后,在拖动目标上,我有

domBox.ondrop = ev => {
            if (!ev.dataTransfer.getData('itemid')) return;
            ev.preventDefault();
            var itemId = ev.dataTransfer.getData("itemid");
            // do stuff with itemid
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM