简体   繁体   English

我在拖放中停止了什么传播?

[英]What propagation I'm stopping in drag and drop?

Following the MDN guide Selecting files using drag and drop , the code is: 遵循MDN指南, 使用拖放选择文件 ,代码为:

var dropbox;

dropbox = document.getElementById("dropbox");
dropbox.addEventListener("dragenter", dragenter, false);
dropbox.addEventListener("dragover", dragover, false);
dropbox.addEventListener("drop", drop, false);

function dragenter(e) {
  e.stopPropagation();
  e.preventDefault();
}

function dragover(e) {
  e.stopPropagation();
  e.preventDefault();
}

function drop(e) {
  e.stopPropagation();
  e.preventDefault();

  var dt = e.dataTransfer;
  var files = dt.files;

  handleFiles(files);
}

I understand that I've to use preventDefault() to avoid the browser from open the file that I drop. 我知道我必须使用preventDefault()来避免浏览器打开我删除的文件。 But why do I've to use stopPropagation() ? 但是,为什么我必须使用stopPropagation() What bubbling/capturing am I stopping here? 我要在这里停下来冒泡吗?

It means 'if there any elements in the DOM containing your "dropbox" id element with matching named event handler functions attached, then they are not notified about the event'. 它的意思是“如果DOM中有任何包含“ dropbox” id元素的元素,并附加了匹配的命名事件处理程序函数,则不会通知该事件。

There is a good example at the Mozilla javascript documentation on the DOM DOM上的Mozilla javascript文档中有一个很好的例子

which has an html table containing a table data cell, both of which have an event handler attached. 它有一个包含表数据单元格的html表,这两个表都附加了事件处理程序。 If you remove the ev.stopPropogation() (at line 17), both event handler functions would be called, with it there, only the table cell event handler is called. 如果删除ev.stopPropogation()(在第17行),则将调用两个事件处理程序函数,并在那里调用,仅调用表单元格事件处理程序。

-- -

There is also a similar SO question at What's the difference between event.stopPropagation and event.preventDefault? 在event.stopPropagation和event.preventDefault之间有什么区别?

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

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