【发布时间】:2016-11-01 21:22:12
【问题描述】:
根据 HTML5 拖放 API 中的the documentation,当元素被拖放时会触发两个事件:
- 从放置目标触发
drop事件 - 从拖动源触发
dragend事件
在做一个简单的测试(见 sn-p)时,drop 事件总是在dragend 事件之前触发(至少在 Chrome 中),但我在规格。
这些事件的顺序是否已定义,或者它们是否可以任意顺序触发?
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
console.log("drop at " + Date.now());
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
function dragend(ev) {
console.log("dragend at " + Date.now());
}
#div1 {
background-color: red;
height: 100px;
width: 100px;
}
#drag1 {
background-color: green;
height: 50px;
width: 50px;
}
<div>Drag the green square in to the red one</div>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" width="100px" height="100px"></div>
<div id="drag1" draggable="true" ondragstart="drag(event)" ondragend="dragend(event)" width="50px" height="50px">
【问题讨论】:
-
火狐的顺序是一样的
标签: javascript html drag-and-drop