简体   繁体   English

拖放后设置图像src

[英]set image src after drag and drop

I have a ul of li elements each with an img.我有一个 ul li 元素,每个元素都有一个 img。

<li class="c-hex-grid__item">
  <img class="c-hex-grid__media" src="/img/hex.png" alt="" id="new-hex_2" ondrop="drop(event)" ondragover="allowDrop(event)" draggable="true" ondragstart="drag(event)"/>
</li>

I'm trying to drag the img to another ul.我正在尝试将 img 拖到另一个 ul。

<ul style="height:600px; overflow:hidden; overflow-y:scroll;" ondrop="drop_list(event)" ondragover="allowDrop(event)" >
  <li draggable="true" id="19" ondragstart="drag(event)">
  </li>
.....

Once I've dropped the img, I don't want to remove the img element and append to the new ul like the usual Drag and Drop.删除 img 后,我不想删除 img 元素并像通常的拖放一样附加到新的 ul。

Instead I need the img element to remain.相反,我需要保留 img 元素。 I then try and change the source of the image, so that the image changes.然后我尝试更改图像的来源,以便图像发生变化。

function drop_list(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    var image = document.getElementById(data).cloneNode(true);
    var img_name = 'img/hex.png';
    image.src = img_name;
}

The item is dragging correctly, and the image var is being correctly set from:该项目正在正确拖动,并且图像变量正在从以下位置正确设置:

document.getElementById(data).cloneNode(true);

And the img_name is correctly finding the image file.并且 img_name 正确地找到了图像文件。

However, it's not setting the image src to the new file.但是,它不会将图像 src 设置为新文件。

I'm not seeing any errors in chrome debugging.我在 chrome 调试中没有看到任何错误。

I wonder if it's because the HTML needs reloading/building?我想知道是不是因为 HTML 需要重新加载/构建?

I removed the cloneNode and it works!我删除了 cloneNode 并且它有效!

function drop_list(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  image = document.getElementById(data);
  var img_name = 'img/hex.png';
  image.src = img_name;
}

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

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