Javascript dragover事件,目标需要是可拖动的元素

Javascript dragover事件,目标需要是可拖动的元素,javascript,Javascript,我需要获取父元素,该元素已将draggable属性分配给它,因为它还有一些我需要的其他数据属性 但是,如果我单击并拖动一个子元素,当然这就是目标 有没有一种方法可以避免可怕的findAncestor循环 要使数据在从“拖动”元素中拖放时可用,必须将其包含在事件中。在跨浏览器实现中,这可能会变得很棘手,尤其是当您必须支持传统(即IE)浏览器时 const startDrag(event) { // 'this' is the 'draggable' you are attaching thi

我需要获取父元素,该元素已将
draggable
属性分配给它,因为它还有一些我需要的其他数据属性

但是,如果我单击并拖动一个子元素,当然这就是目标

有没有一种方法可以避免可怕的
findAncestor
循环


要使数据在从“拖动”元素中拖放时可用,必须将其包含在事件中。在跨浏览器实现中,这可能会变得很棘手,尤其是当您必须支持传统(即IE)浏览器时

const startDrag(event) {
  // 'this' is the 'draggable' you are attaching this method to,
  // so if you need the data attribute just use this.getAttribute()
  // get it.
  // get the data you need to transfer with your object, and then
  event.dataTransfer.setData('text', JSON.stringify(somesetofdata));
}

const drop(event) {
  let data = event.dataTransfer.getData('text');
  data = data ? JSON.parse(data) : data;
  // do something with it
}

这就是要点。谷歌“纯Javascript拖放”提供了大量示例代码。这是一个。

到目前为止你都试过什么?我们无法为您编写代码,但我们可以帮助您调试所编写的代码。