How can I take elements out of the DOM, after pasting or dropping something?

荒凉一梦 提交于 2020-08-05 09:37:33

问题


I am using jQuery's remove() function to delete everything that is pasted/dropped in a contenteditable area, except for text. I am especially interested in disabling images.

I tried it this way

function removeImg() {
  setInterval(function() {
    $("#editableDiv").contents(":not(p)").remove();
  }, 1);
}
#editableDiv {
  padding-left: 10px;
  width: 100%;
  border: solid 1px blue;
}

img {
  width: 100px;
  margin-right: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="editableDiv" contenteditable onpaste="removeImg();" ondrop="removeImg();">
  <p>Images that you paste or drop here should be automatically removed.</p>
</div>
<hr>
<p>You can use these images:</p>
<img src="https://i7.pngguru.com/preview/134/138/533/star-golden-stars.jpg">
<img src="https://i7.pngguru.com/preview/134/138/533/star-golden-stars.jpg">
<p>Either copying-pasting or dragging-dropping them don't work...</p>

However, it is not working, and I do not understand why. The console shows no errors. I tried it on both Firefox and Chrome's latest versions.

Can someone help me out with this?


回答1:


I think you don't need the interval for it. Just set the content with

function removeImg() {
document.getElementById("editableDiv").innerHTML = "<p>Images that you paste or drop here should be automatically removed.</p>";
}

What you can also do, is when the text changes, save it to a variable and then load it when an image is dropped.

var content;
document.getElementById("editableDiv").oninput = function() {
content = document.getElementById("editableDiv").innerHTML;
}
function removeImg() {
document.getElementById("editableDiv").innerHTML = content;
}

Hope this helped!




回答2:


I think you want to remove elements from body not contenteditable div? Then you can do it like this:

-Find all images, add addEventListener mousedown

-on mouse down add auto incremented id, in this example:clicked2 ++ numbers. You do this in order that you have unique id even if user does not drop item. On every mouse down new id is added on clicked element. If id does not suit you you can do classes or anything really.

-Then you call your function on drop to remove element if its drooped with last generated id.

You can apply same logic to any element selector, not just img, just make selector document.querySelectorAll('body *')

var i = 0;
var removeID="";
[...document.querySelectorAll('img')].forEach(img => {
  img.addEventListener('mousedown', event => {
    img.id = "clicked"+(++i);
    console.clear();    
    removeID=img.id;
    console.log(removeID);
  })
})
function removeImg() {
  document.getElementById(removeID).remove();
}
#editableDiv {
  padding-left: 10px;
  width: 100%;
  border: solid 1px blue;
}

img {
  width: 100px;
  margin-right: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="editableDiv" contenteditable onpaste="removeImg();" ondrop="removeImg();">
  <p>Images that you paste or drop here should be automatically removed.</p>
</div>
<hr>
<p>You can use these images:</p>
<img src="https://i7.pngguru.com/preview/134/138/533/star-golden-stars.jpg">
<img src="https://i7.pngguru.com/preview/134/138/533/star-golden-stars.jpg">
<p>Either copying-pasting or dragging-dropping them don't work...</p>

Example using classes and all elements:

var i = 0;
var removeClass="";
[...document.querySelectorAll('body *')].forEach(el => {
  el.addEventListener('mousedown', event => {
    el.classList.add("clicked"+(++i));
    console.clear();    
    removeClass="clicked"+(+i);
    console.log(removeClass)
  })
})
function removeImg() {
  document.querySelector("."+removeClass).remove();
}
#editableDiv {
  padding-left: 10px;
  width: 100%;
  border: solid 1px blue;
}

img {
  width: 100px;
  margin-right: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="editableDiv" contenteditable onpaste="removeImg();" ondrop="removeImg();">
  <p>Images that you paste or drop here should be automatically removed.</p>
</div>
<hr>
<p>You can use these images:</p>
<img src="https://i7.pngguru.com/preview/134/138/533/star-golden-stars.jpg">
<img src="https://i7.pngguru.com/preview/134/138/533/star-golden-stars.jpg">
<p>Either copying-pasting or dragging-dropping them don't work...</p>



回答3:


None of the answers provided a solution to what I was trying to achieve. Ended up fixing it by changing this:

function removeImg() {
  setInterval(function() {
    $("#editableDiv").contents(":not(p)").remove();
  }, 1);
}

To this:

function removeImg() {
  setTimeout(function() {
    $('#editableDiv :not(p)').remove();
  }, 1);
}


来源:https://stackoverflow.com/questions/63118988/how-can-i-take-elements-out-of-the-dom-after-pasting-or-dropping-something

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!