javascript 使用 AngularJs 将 html 内容作为纯文本粘贴到 contenteditable div 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27733025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Paste html content as plain text in contenteditable div using AngularJs
提问by jsbisht
I want to paste text selected from a certain document(pdf, docx, html), into a div of contenteditable type.
我想将从某个文档(pdf、docx、html)中选择的文本粘贴到 contenteditable 类型的 div 中。
Now I want to remove all the formatting of the clipboard text before it is rendered. So, the final content pasted should be a plain text.
现在我想在呈现之前删除剪贴板文本的所有格式。所以,最终粘贴的内容应该是纯文本。
An analogue of this scenario can be pasting content into Windows Notepad.
这种情况的类似物可以是将内容粘贴到 Windows 记事本中。
How could this be done using AngularJs. Or there exist any other javascript library to acomplish this.
如何使用 AngularJs 做到这一点。或者存在任何其他 javascript 库来实现这一点。
Update:I can use the following code to get the clipboard as a text.
更新:我可以使用以下代码将剪贴板作为文本获取。
editor.addEventListener("paste", function(e) {
// cancel paste
e.preventDefault();
// get text representation of clipboard
var text = e.clipboardData.getData("text/plain");
// insert text manually
document.execCommand("insertHTML", false, text);
});
But i dont know how and where to add this code in AngularJs.
但我不知道如何以及在何处在 AngularJs 中添加此代码。
回答by Peter Ashwell
Check this answer out, you can capture paste events. Doesn't matter if it's content editable div or otherwise.
检查此答案,您可以捕获粘贴事件。它是内容可编辑的 div 或其他方式都没有关系。
JavaScript get clipboard data on paste event (Cross browser)