[英]How to prevent a contenteditable div from rendering html?
Here is my code: https://jsfiddle.net/tb8ryyp6/1/ 这是我的代码: https : //jsfiddle.net/tb8ryyp6/1/
As you can see if we drag and drop image to contenteditable div
its content is rendered as HTML. 如您所见,如果我们将图像拖放到contenteditable
div
其内容将呈现为HTML。 I want to prevent this behavior. 我想防止这种行为。 Currently I solved this problem with attaching event handler to contenteditable
div
on drop event like this ondrop='return false;'
目前,我通过将事件处理程序附加到drop事件上的contenteditable
div
来解决此问题,例如ondrop='return false;'
. 。
This works for me. 这对我有用。
<div contenteditable='true' ondrop='return false;'>
But . 但是 。 This solution seems not to be a cross-browser solution and user can remove this even handler easily at any time by changing HTML source code in dev-tools.
该解决方案似乎不是跨浏览器的解决方案,用户可以随时通过在dev-tools中更改HTML源代码来轻松删除此偶数处理程序。
In general I want to prevent a contenteditable div
from rendering any HTML element. 总的来说,我想防止contenteditable
div
呈现任何HTML元素。 This element could be not just image it could be an anchor element for example. 例如,此元素可能不仅是图像,还可能是锚元素。 As solution it can be both a cross-browser preventing of drop actions or showing HTML as text.
作为解决方案,它既可以是跨浏览器防止拖放操作,也可以是将HTML显示为文本。 But first one is preferable.
但是第一个是可取的。
Thanks for your help! 谢谢你的帮助!
In most cases, you can design a <textarea>
just like anything else. 在大多数情况下,您可以像其他任何东西一样设计
<textarea>
。 But what you want can also be done with a <div contenteditable>
. 但是,您也可以使用
<div contenteditable>
。
user can remove this even handler at any time by changing html source code in dev-tools.
用户可以随时通过在dev-tools中更改html源代码来删除此偶数处理程序。
If you use addEventListener("drop", func)
, the event listener isn't visible in the html code. 如果使用
addEventListener("drop", func)
,则事件侦听器在html代码中不可见。 It can still be removed though, but then it's not your problem if the website doesn't work properly anymore (it's only changed in the browser, not on the server!) 虽然仍然可以将其删除,但是如果网站无法正常运行,那就不是您的问题了(仅在浏览器中更改,而不在服务器上更改!)
This code uses the innerText
property (which contains the unformatted text). 此代码使用
innerText
属性(包含未格式化的文本)。 Since line breaks are \\n
in innerText
and <br>
in the <div>
, it has to be converted. 由于
innerText
换行符是\\n
,而<div>
中的<br>
是换行符,因此必须对其进行转换。
var ediv = document.getElementById("ediv"); function noHTML() { setTimeout(function() { var str1 = ediv.innerText.replace(/\\n/g,""); var str2 = ediv.innerHTML.replace(/<br>/g,""); if (str1 != str2) { ediv.innerText = ediv.innerText; } }, 1); } // Remove markup when dropping something (eg image/ link) ediv.addEventListener("drop", noHTML); // Remove markup when pasting something (eg from Microsoft Word) ediv.addEventListener("paste", noHTML); // Remove markup from the clipboard when copying something ediv.addEventListener("copy", function(e) { noHTML(); if (e.clipboardData) { var text = window.getSelection().toString(); e.preventDefault(); e.clipboardData.setData('Text', text); } }); // Remove markup when the div gains focus ediv.addEventListener("focus", noHTML); // Remove markup when the div loses focus ediv.addEventListener("blur", noHTML);
#ediv{ background-color: white; height: 70px; border: 2px dotted blue; margin-bottom: 20px; padding: 10px; } [placeholder]:empty:before { content: attr(placeholder); color: #ddd; } [placeholder]:empty:focus:before { content: ""; } #img { text-align: center; }
<div id='ediv' contenteditable='true' placeholder='i am contenteditable div..'></div> <div id='img'> <span>drag and drop this image to contenteditable div</span> <br> <img src='https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_March_2010-1.jpg' width='220px'><br> <a href='https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_March_2010-1.jpg'>Hyperlink</a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.