javascript - 粘贴到 contenteditable 会导致随机标签插入

标签 javascript html contenteditable

我正在使用一个 contentedittable 字段作为用户输入,这样我就可以利用文本格式。不幸的是,我发现当用户粘贴到字段中时,会出现大量不必要的 html。我只想要剪贴板中的纯文本。

为什么会这样?!

chop 示例:

<div><br></div><div><span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 12px; color: rgb(0, 0, 0); "><table style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-position: initial initial; background-repeat: initial initial; "><tbody style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; background-position: initial initial; background-repeat: initial initial; "><tr style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; background-position: initial initial; background-repeat: initial initial; "><td class="votecell" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; vertical-align: top; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; width: 60px; background-position: initial initial; background-repeat: initial initial; "><div class="vote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; text-align: center; background-position: initial initial; background-repeat: initial initial; "><span class="vote-count-post" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 31px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; display: block; color: rgb(128, 129, 133); font-weight: bold; background-position: initial initial; background-repeat: initial initial; ">1</span><a class="vote-down-off" title="This question does not show any research effort; it is unclear or not useful (click again to undo)" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 1px; vertical-align: baseline; background-image: url(http://sstatic.net/stackoverflow/img/sprites.png?v=3); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: rgb(0, 119, 204); text-decoration: none; cursor: pointer; overflow-x: hidden; overflow-y: hidden; display: block; width: 41px; height: 25px; text-indent: -9999em; background-position: 0px -300px; background-repeat: no-repeat no-repeat; ">...

最佳答案

您只能使用 hack 获取纯文本。 TinyMCE 和 CKEditor 的最新版本都在其基于 iframe 的编辑器上使用了这种技术:

  1. 使用按键事件处理程序检测 Ctrl-v/Shift-Insert 事件
  2. 在该处理程序中,保存当前用户选择,在屏幕外(例如左侧 -1000px)向文档添加一个文本区域元素,关闭 contentEditable 并调用 focus() 在文本区域上,从而移动插入符并有效地重定向粘贴
  3. 在事件处理程序中设置一个非常短的计时器(比如 1 毫秒)以调用另一个存储文本区域值的函数,从文档中删除文本区域,重新打开 contentEditable,恢复用户选择并将文本粘贴到。

请注意,这仅适用于键盘粘贴事件,不适用于上下文或编辑菜单中的粘贴。 paste 事件会更好,但是当它触发时,在大多数浏览器中将插入符号重定向到文本区域已经太晚了。

关于javascript - 粘贴到 contenteditable 会导致随机标签插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6022551/

相关文章:

html - Bootstrap 网格 - 在列中设置相对行高

javascript - 模态不关闭视频的嵌入式 iframe

javascript - 为什么 JQuery on() 事件处理程序不使用 contenteditable 从动态生成的元素中捕获事件?

javascript - 将 CodeMirror 应用于预先存在的 div

javascript - Google Apps 脚本 : How to check if a calendar event has been marked as deleted manually

javascript - 替换或管理ajax请求中的json数据

html - 在移动设备中隐藏水平滚动条

html - 如何使 HTML5 contenteditable div 只允许 firefox 中的文本?

javascript - 如何为 href 背景设置替代颜色

javascript - 表单未在 javascript 中验证