我在我的文本编辑器中使用CLEditor,http://premiumsoftware.net/cleditor/。有一个“粘贴为文本”的功能,当点击按钮时,会出现一个弹出窗口,里面有一个文本区域,可以粘贴你的内容,样式也会被剥离。
我想要做的是使用相同的功能,但是任何时候有人粘贴到主文本区域,这个操作都会自动执行。我该怎么触发它呢?
我已经创建了一个JS文件http://jsfiddle.net/helpinspireme/naubS/,但无法将所有JS粘贴到其中,因此无法链接到CLEditor的主JS文件,请访问他们的站点:http://premiumsoftware.net/cleditor/
谢谢你的帮助。
发布于 2013-03-04 19:24:23
我知道我没有回答您的实际问题,但如果您的真正问题是用户尝试从Microsoft Office (例如Word)粘贴文本时生成的垃圾,我建议您考虑另一种解决方案。
CLEditor本身可以在iFrame (富文本模式)和文本区(源模式)之间切换。“粘贴为文本”功能使用了一个文本区,该文本区本身不支持富文本,因此它一开始就不允许垃圾html。
然而,如果编辑器处于富文本模式,则很难防止用户从Word中粘贴(他可以使用常规的粘贴按钮,按CTRL-V,甚至使用鼠标右键上下文菜单,这些都是不同的事件,很难使用javascript截获)。现在损害已经造成了;你的编辑器里有乱七八糟的html。因此,我没有尝试清理Word产生的垃圾,而是在保存捕获的输入时实现了以下检查(javascript):
if(clEditorValue && (clEditorValue.indexOf('<!--') !== -1 || clEditorValue.indexOf('mso-ansi-language') !== -1 ) ) {
alert('Unable to process text pasted from Word, please use "Paste as text" or modify your input');
return;
}我希望这个答案对其他试图实现同样目标的人有用。
发布于 2017-02-08 09:19:02
当粘贴到cleditor中时,编辑器会从源代码中提取所有的html。我最终编辑了jquery.cleditor.js,并在$doc.click(hidePopups)函数中添加了一个绑定函数。我使用setTimeouts来允许文本填充输入,并使用updateTextArea函数来完成。
.bind("paste", function() {
setTimeout(function() {
refreshButtons(editor);
updateTextArea(editor, true);
//remove any and all html from the paste action
setTimeout(function() {
//clean up the html with removeHtml function
$(editor.doc.body).html(removeHtml($(editor.doc.body).html()));
//change the input value with new clean string
$("#" + editor.$area[0].id).val($(editor.doc.body).html());
}, 100);
}, 100);
})我使用下面的removeHtml函数从粘贴的内容中删除所有超文本标记语言。
//remove html altogether
function removeHtml(str) {
var regex = /(<([^>]+)>)/ig;
var result = str.replace(regex, "");
return result;
}此解决方案现已投入生产。
https://stackoverflow.com/questions/9913794
复制相似问题