Javascript 在summernote编辑器中将内容粘贴为纯文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30993836/
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 content as plain text in summernote editor
提问by Revathy kr
I need to copy paste some content in my summernote editor .But when I paste, it takes the style of the page from where I have copied it.I need to paste it as plain text. Is there any solution?
我需要在我的summernote编辑器中复制粘贴一些内容。但是当我粘贴时,它采用了我复制它的页面样式。我需要将其粘贴为纯文本。有什么解决办法吗?
回答by jcuenod
Use the onPasteCallback
使用onPaste回调
Use the onPasteoption to define a callback that will strip the tags and manually insert the text.
使用该onPaste选项定义一个回调,该回调将剥离标签并手动插入文本。
$editor.summernote({
onPaste: function (e) {
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
document.execCommand('insertText', false, bufferText);
}
});
Credit: https://github.com/summernote/summernote/issues/303
信用:https: //github.com/summernote/summernote/issues/303
Solve Firefox Problems:
解决火狐问题:
You may still have problems with Firefox. If so, wrap document.execCommandin a timer function to delay its execution a tiny bit:
您可能仍然遇到 Firefox 问题。如果是这样,请包装document.execCommand一个计时器函数以稍微延迟其执行:
setTimeout(function(){
document.execCommand( 'insertText', false, bufferText );
}, 10);
Update for v0.7.0+
v0.7.0+ 更新
Position of callbacks in options is changed after v0.7.0
After v0.7.0, every callbacks should be wrapped by callbacks object. (source)
选项中回调的位置在 v0.7.0
之后发生了变化 v0.7.0之后,每个回调都应该被回调对象包装。(来源)
This means that the original code should be written as
这意味着原始代码应该写成
$editor.summernote({
callbacks: {
onPaste: function (e) {
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
document.execCommand('insertText', false, bufferText);
}
}
});
Credit to Mathieu Castets for pointing this out (so if this bit helped, upvote his answer!)
感谢 Mathieu Castets 指出这一点(所以如果这有帮助,请为他的回答点赞!)
TL;DR:Here's a functional demo
TL;DR:这是一个功能演示
回答by Mathieu Castets
After v0.7.0, every callbacks should be wrapped by callbacks object. http://summernote.org/deep-dive/#callbacks
在 v0.7.0 之后,每个回调都应该被回调对象包裹。 http://summernote.org/deep-dive/#callbacks
So if you are using summernote from v0.7.0 or above, jcuenod's answer could now be rewritten as:
因此,如果您使用的是 v0.7.0 或更高版本的 summernote,现在可以将 jcuenod 的答案改写为:
$('.summernote').summernote({
callbacks: {
onPaste: function (e) {
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
// Firefox fix
setTimeout(function () {
document.execCommand('insertText', false, bufferText);
}, 10);
}
}
});
回答by Theo
The onPaste-callback works great but I was having problems with the different handling of linebreaks in different browsers. So I came up with the following solution, which uses html-linebreaks:
onPaste-callback 效果很好,但我在不同浏览器中对换行符的不同处理遇到了问题。所以我想出了以下解决方案,它使用 html-linebreaks:
$(".htmleditor").summernote({
callbacks: {
// callback for pasting text only (no formatting)
onPaste: function (e) {
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
bufferText = bufferText.replace(/\r?\n/g, '<br>');
document.execCommand('insertHtml', false, bufferText);
}
}
});
回答by Tom Glover
Managed to make a horrible hack work for IE11. This will sadly also ask for a paste permission from the user as well. If someone figures out a better suggestion I'm all ears.
设法为 IE11 做了一个可怕的黑客工作。遗憾的是,这也会要求用户获得粘贴许可。如果有人想出更好的建议,我会全神贯注。
var trap = false;
$(document).ready(function () {
$('#summernote').summernote({
callbacks: {
onPaste: function (e) {
if (document.queryCommandSupported("insertText")) {
var text = $(e.currentTarget).html();
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
setTimeout(function () {
document.execCommand('insertText', false, bufferText);
}, 10);
e.preventDefault();
} else { //IE
var text = window.clipboardData.getData("text")
if (trap) {
trap = false;
} else {
trap = true;
setTimeout(function () {
document.execCommand('paste', false, text);
}, 10);
e.preventDefault();
}
}
}
}
})
})
回答by Reza Rouf
ctrl+shift+V is the easiest solution :D
ctrl+shift+V 是最简单的解决方案:D

