HTML事件属性ondragend

HTML事件属性ondragend


触发 ondragend 属性事件当完成拖动元素或文本选择时。

要使元素可拖动,请标记该元素与全局HTML5 draggable 属性。

默认情况下,链接和图像是可拖动的。

在draggable源元素上触发的事件:

  • ondragstart - starting dragging
  • ondrag - during the dragging
  • ondragend - finish dragging

投放目标上触发的事件:

  • ondragenter - when the dragged element enters the droppable target
  • ondragover - when the dragged element is over the droppable target
  • ondragleave - when the dragged element leaves the droppable target
  • ondrop - when the dragged element is dropped on the droppable target


HTML5中的新功能

ondragend 属性是HTML5中的新特性。

句法

<element ondragend="script">

支持的标签

所有HTML元素

浏览器兼容性

ondragend Yes 9.0 Yes Yes Yes

例子

<!DOCTYPE HTML>
<html>
<head>
<style>
.droptarget {
    float: left; 
    width: 100px; 
    height: 35px;
    margin: 15px;
    padding: 10px;
    border: 1px solid red;
}
</style>
</head>
<body>

<div class="droptarget" ondrop="drop(event)" ondragover="allowDrop(event)">
  <p ondragstart="dragStart(event)" 
     ondrag="dragging(event)" 
     draggable="true" 
     ondragend="dragEnd(event)" 
     id="dragtarget">Drag me!</p>
</div>

<div class="droptarget" 
    ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<script>
function dragStart(event) {
    event.dataTransfer.setData("Text", event.target.id);
}
function dragEnd(event) {
    console.log("Finished dragging.");
}

function dragging(event) {
    console.log("being dragged");
}

function allowDrop(event) {
    event.preventDefault();
}

function drop(event) {
    event.preventDefault();
    var data = event.dataTransfer.getData("Text");
    event.target.appendChild(document.getElementById(data));
    console.log("dropped");
}
</script>

</body>
</html>

Click to view the demo