简体   繁体   English

停止在 contenteditable div 中粘贴 html 样式,只粘贴纯文本

[英]Stop pasting html style in a contenteditable div only paste the plain text

I am using a contenteditable div, when I tried to paste something with style, it was suppose to only copy the plain text, but it got the style as well, does anyone know how to force it to convert to plain text when I paste it or anyone has a better solution我正在使用 contenteditable div,当我尝试粘贴带有样式的内容时,它应该只复制纯文本,但它也有样式,有没有人知道如何在我粘贴时强制它转换为纯文本或者谁有更好的解决方案

Here is my code:这是我的代码:

    <div contenteditable="true">   
      This text can be edited by the user. 
    </div>

在此处输入图像描述 在此处输入图像描述

When you paste in rich content, it will be displayed as rich content.当您粘贴富内容时,它将显示为富内容。 So you would need to capture the paste event, prevent the default action, and read the text from the clipboard.因此,您需要捕获粘贴事件、阻止默认操作并从剪贴板读取文本。

 var ce = document.querySelector('[contenteditable]') ce.addEventListener('paste', function (e) { e.preventDefault() var text = e.clipboardData.getData('text/plain') document.execCommand('insertText', false, text) })
 [contenteditable] { background-color: black; color: white; width: 400px; height: 200px; }
 <div contenteditable="true"></div> <div> <h1>Test content</h1> <p style="color:red">Copy <em>this</em> <u>underlined</u></p> </div>

You can intercept the "paste" event and replace the content of the target.您可以拦截“粘贴”事件并替换目标的内容。

 /* Derrived from: https://stackoverflow.com/a/6035265/1762224 */ const onPastePlainText = (e) => { var pastedText = undefined; if (window.clipboardData && window.clipboardData.getData) { // IE pastedText = window.clipboardData.getData('Text'); } else if (e.clipboardData && e.clipboardData.getData) { pastedText = e.clipboardData.getData('text/plain'); } e.target.textContent = pastedText; e.preventDefault(); return false; } document.querySelector('.ediatable-div').addEventListener('paste', onPastePlainText);
 .ediatable-div { border: 2px inset #EEE; height: 25vh; } /* Placeholder - Derrived from: https://stackoverflow.com/a/20300212/1762224 */ [contentEditable=true]:empty:not(:focus):before { content: attr(data-text); color: #AAA; }
 <div class="ediatable-div" contenteditable="true" data-text="Paste copied HTML here"></div> <div> <p style="text-decoration:underline">Copy <strong>me</strong>, I have <em>style</em>!</p> </div>

The following solution works when there is already text in the field that is being pasted into, and it does not use the deprecated document.execCommand() function.当要粘贴到的字段中已有文本时,以下解决方案有效,并且它不使用已弃用的document.execCommand() function。

 // Edited from https://htmldom.dev/paste-as-plain-text/ const editable_div = document.getElementById('editable_div'); // Handle the paste event editable_div.addEventListener('paste', function (e) { // Prevent the default action e.preventDefault(); // Get the copied text from the clipboard const text = e.clipboardData? (e.originalEvent || e).clipboardData.getData('text/plain'): // For IE window.clipboardData? window.clipboardData.getData('Text'): ''; // Insert text at the current position of caret const range = document.getSelection().getRangeAt(0); range.deleteContents(); const textNode = document.createTextNode(text); range.insertNode(textNode); range.selectNodeContents(textNode); range.collapse(false); const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); });
 <div id="editable_div" contenteditable="true"> You can only paste plain text into me! </div>

When contenteditable elements might be dynamically added to the DOM, it is useful to have a document-level handler that, once there, will detect future instances as well as existing ones.当 contenteditable 元素可能被动态添加到 DOM 时,拥有一个文档级处理程序很有用,一旦存在,它将检测未来的实例以及现有的实例。

// 1. Paste plain text only into contenteditable elements
document.addEventListener("paste", function (e) {
    if (e.target.isContentEditable) {
        e.preventDefault();
        var text = e.clipboardData.getData('text/plain')
        document.execCommand('insertText', false, text)
    }
});

// 2. Prevent any paste into contenteditable elements
document.addEventListener("paste", function (e) {
    if (e.target.isContentEditable) {
        e.preventDefault();
        return false;
    }
});

I just tried to reproduce the issue on http://jsbin.com/ and the resulting div didn't have any styles on it.我只是试图在http://jsbin.com/上重现该问题,结果 div 上没有任何 styles 。 The issue you're seeing might be coming from Codepen rather than the HTML tag itself applying styles.您看到的问题可能来自 Codepen,而不是 HTML 标签本身应用 styles。

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

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