Zdarzenie ondrop

Definicja i użycie

Kiedy przeciągany element lub wybrany tekst jest umieszczony na poprawnym celu umieszczania, wywoływane jest wydarzenie ondrop.

Przeciąganie i upuszczanie to bardzo popularna funkcja w HTML5. Oznacza to, że gdy "chwytasz" obiekt i przeciągasz go do innej pozycji. Aby dowiedzieć się więcej, przeczytaj nasze informacje o Przenoszenie HTML5HTML przewodnika.

Komentarz:Aby uczynić element przeciągalnym, użyj globalnego Atrybut draggable HTML5

Wskazówka:Domyślnie, linki i obrazy są przeciągane, nie wymagają atrybutu draggable.

W różnych etapach operacji przeciągania i upuszczania używane są wiele wydarzeń i mogą wystąpić:

Wydarzenia wywoływane na elemencie docelowym (element źródłowy):

  • ondragstart - gdy użytkownik rozpocznie przeciąganie elementu
  • ondrag - gdy element jest przeciągany
  • ondragend - gdy użytkownik zakończy przeciąganie elementu

Wydarzenia wywoływane na celu umieszczania:

  • ondragenter - gdy przeciągany element wchodzi w cel umieszczania
  • ondragover - gdy przeciągany element znajduje się na celu umieszczania
  • ondragleave - gdy przeciągany element opuszcza cel umieszczania
  • ondrop - gdy przeciągany element zostaje umieszczony na celu umieszczania

Przykład

W elemencie <div> umieszczaj elementy do przeciągania i upuszczania, aby uruchomić JavaScript:

<div ondrop="myFunction(event)"></div>

Try it yourself

Poniżej znajdują się więcej przykładów TIY.

Gramatyka

W HTML:

<element ondrop="myScript">

Try it yourself

W JavaScript:

obiekt.ondrop = function(){myScript};

Try it yourself

W JavaScript, używając metody addEventListener():

obiekt.addEventListener("drop", myScript);

Try it yourself

Komentarz:Internet Explorer 8 lub wcześniejsze wersje nie obsługują addEventListener() 方法

技术细节

冒泡: 支持
可取消: 支持
事件类型: DragEvent
支持的 HTML 标签: 所有 HTML 元素
DOM 版本: Level 3 Events

浏览器支持

表中的数字注明了完全支持该事件的首个浏览器版本。

事件 Chrome IE Firefox Safari Opera
ondrop 4.0 9.0 3.5 6.0 12.0

更多实例

对所有可能的拖放事件的演示:

<p draggable="true" id="dragtarget">Drag me!</p>
<div class="droptarget">Drop here!</div>
<script>
/* ----------------- 在拖动目标上触发的事件 ----------------- */
document.addEventListener("dragstart", function(event) {
  // dataTransfer.setData()方法设置被拖拽数据的数据类型和值
  event.dataTransfer.setData("Text", event.target.id);
  // 开始拖动 p 元素时输出一些文本
  document.getElementById("demo").innerHTML = "Started to drag the p element.";
  // 更改可拖动元素的不透明度
  event.target.style.opacity = "0.4";
});
// 拖动 p 元素时,更改输出文本的颜色
document.addEventListener("drag", function(event) {
  document.getElementById("demo").style.color = "red";
});
// 完成拖动 p 元素后输出一些文本并重置不透明度
document.addEventListener("dragend", function(event) {
  document.getElementById("demo").innerHTML = "Finished dragging the p element.";
  event.target.style.opacity = "1";
});
/* ----------------- Events triggered on the drop target ----------------- */
// When the draggable p element enters the droptarget, change the border style of the DIV
document.addEventListener("dragenter", function(event) {
  if ( event.target.className == "droptarget" ) {
    event.target.style.border = "3px dotted red";
  }
});
// By default, data/elements cannot be placed in other elements. To allow placement, we must prevent the default handling of the element
document.addEventListener("dragover", function(event) {
  event.preventDefault();
});
// When the draggable p element leaves the droptarget, reset the border style of the DIV
document.addEventListener("dragleave", function(event) {
  if ( event.target.className == "droptarget" ) {
    event.target.style.border = "";
  }
});
/* On drop - prevent the browser's default handling of data (open as a link when dropped by default) */
Reset the color of the output text and the border color of the DIV
Use the dataTransfer.getData() method to get the dragged data
The dragged data is the id of the dragged element ("drag1")
Attach the draggable element to the drop element
*/
document.addEventListener("drop", function(event) {
  event.preventDefault();
  if ( event.target.className == "droptarget" ) {
    document.getElementById("demo").style.color = "";
    event.target.style.border = "";
    var data = event.dataTransfer.getData("Text");
    event.target.appendChild(document.getElementById(data));
  }
});
</script>

Try it yourself

Related pages

HTML tutorial: HTML5 drag and drop

HTML referency manual: HTML draggable attribute