简体   繁体   English

如何防止contenteditable div呈现html?

[英]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> 

Updated Fiddle 更新小提琴

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

相关问题 如何防止红色波浪线从一个不再聚焦的可疑div? - How to prevent red squiggle lines from a contenteditable div that is no longer in focus? 如何防止从中删除“部分”<div contenteditable> 编辑的时候? - How to prevent removing "section" from <div contenteditable> when editing? 如何防止 CSS 被粘贴到 contenteditable div - How to prevent CSS from being pasted in a contenteditable div 如何防止浏览器在 contenteditable div 中生成 html 标签 - How to prevent browser from generating html tags in contenteditable divs 如何在原始文本中间粘贴HTML单词后,防止插入符号在contenteditable div中跳到整个文本的末尾? - How to prevent the caret jumping to the end of the whole text in contenteditable div after pasting the HTML words in the middle of the original text? 将textarea中的html粘贴到Contenteditable Div - Paste html from textarea to Contenteditable Div 如何获取内容可编辑 div 的多行 html - how to get multiline html of a contenteditable div 如何在 html 中突出显示 contenteditable div 中的特定单词 - How to highlight specific words in contenteditable div in html 如何阻止“输入”在标记为contentEditable的div中工作? - How do i prevent 'Enter' from working in a div thats marked contentEditable? 防止删除 contenteditable div 中的第一段 - Prevent from deleting the first paragraph inside a contenteditable div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM