我正在使用一个 <div/>
标签,并启用了 PASTE 功能。
从 Microsoft Word 的剪贴板复制粘贴时,会发现大量的标记代码被粘贴进来。我在努力解决这个问题,在使用 Prototypes 的 stripTags()
函数时已经实现了一半(不幸的是,这个函数似乎无法保留某些标签)。
然而,即使如此,我仍然有大量不需要的标记代码。
因此,我的问题是,是否有一些可以清除大部分不必要标记代码的函数(使用 JavaScript)或方法?
我正在使用一个 <div/>
标签,并启用了 PASTE 功能。
从 Microsoft Word 的剪贴板复制粘贴时,会发现大量的标记代码被粘贴进来。我在努力解决这个问题,在使用 Prototypes 的 stripTags()
函数时已经实现了一半(不幸的是,这个函数似乎无法保留某些标签)。
然而,即使如此,我仍然有大量不需要的标记代码。
因此,我的问题是,是否有一些可以清除大部分不必要标记代码的函数(使用 JavaScript)或方法?
这是我编写的函数,据我所知,它可以很好地完成任务。
如果有任何改进建议,欢迎提出。谢谢。
function cleanWordPaste( in_word_text ) {
var tmp = document.createElement("DIV");
tmp.innerHTML = in_word_text;
var newString = tmp.textContent||tmp.innerText;
// this next piece converts line breaks into break tags
// and removes the seemingly endless crap code
newString = newString.replace(/\n\n/g, "<br />").replace(/.*<!--.*-->/g,"");
// this next piece removes any break tags (up to 10) at beginning
for ( i=0; i<10; i++ ) {
if ( newString.substr(0,6)=="<br />" ) {
newString = newString.replace("<br />", "");
}
}
return newString;
}
希望这对你中的一些人有所帮助。
我正在使用这个:
$(body_doc).find('body').bind('paste',function(e){
var rte = $(this);
_activeRTEData = $(rte).html();
beginLen = $.trim($(rte).html()).length;
setTimeout(function(){
var text = $(rte).html();
var newLen = $.trim(text).length;
//identify the first char that changed to determine caret location
caret = 0;
for(i=0;i < newLen; i++){
if(_activeRTEData[i] != text[i]){
caret = i-1;
break;
}
}
var origText = text.slice(0,caret);
var newText = text.slice(caret, newLen - beginLen + caret + 4);
var tailText = text.slice(newLen - beginLen + caret + 4, newLen);
var newText = newText.replace(/(.*(?:endif-->))|([ ]?<[^>]*>[ ]?)|( )|([^}]*})/g,'');
newText = newText.replace(/[·]/g,'');
$(rte).html(origText + newText + tailText);
$(rte).contents().last().focus();
},100);
});
是可编辑的iframe,如果您使用可编辑的div,则可以省略.find('body')部分。基本上它检测到粘贴事件,检查位置,清理新文本,然后将清理后的文本放回粘贴的位置。(听起来有点混乱...但实际上并不像听起来那么糟糕。如果有一个“粘贴为纯文本”按钮,它会显示一个<textarea>
,允许用户将文本粘贴到其中,这样,所有标签都将被剥离。这就是我在我的CMS中所做的;我放弃了尝试清理Word的混乱。
Remove head tag
Remove script tags
Remove styles tag
let clipboardData = event.clipboardData || window.clipboardData;
let pastedText = clipboardData.getData('text/html');
pastedText = pastedText.replace(/\<head[^>]*\>([^]*)\<\/head/g, '');
pastedText = pastedText.replace(/\<script[^>]*\>([^]*)\<\/script/g, '');
pastedText = pastedText.replace(/\<style[^>]*\>([^]*)\<\/style/g, '');
// pastedText = pastedText.replace(/<(?!(\/\s*)?(b|i|u)[>,\s])([^>])*>/g, '');
var cleanDom = function(result, n) {
var nn = n.nodeName;
if(nn=="#text") {
var text = n.nodeValue;
}
else {
if(nn=="A" && n.href)
...;
else if(nn=="IMG" & n.src) {
....
}
else if(nn=="DIV") {
if(n.className=="indent")
...
}
else if(nn=="FONT") {
}
else if(nn=="BR") {
}
if(!UNSUPPORTED_ELEMENTS[nn]) {
if(n.childNodes.length > 0)
for(var i=0; i<n.childNodes.length; i++)
cleanDom(result, n.childNodes[i]);
}
}
}
这个方法非常适用于从HTML文本中删除任何注释,包括来自Word的注释:
function CleanWordPastedHTML(sTextHTML) {
var sStartComment = "<!--", sEndComment = "-->";
while (true) {
var iStart = sTextHTML.indexOf(sStartComment);
if (iStart == -1) break;
var iEnd = sTextHTML.indexOf(sEndComment, iStart);
if (iEnd == -1) break;
sTextHTML = sTextHTML.substring(0, iStart) + sTextHTML.substring(iEnd + sEndComment.length);
}
return sTextHTML;
}
我曾经遇到过类似的问题,换行符被计算为字符,我不得不将它们删除。
$(document).ready(function(){
$(".section-overview textarea").bind({
paste : function(){
setTimeout(function(){
//textarea
var text = $(".section-overview textarea").val();
// look for any "\n" occurences and replace them
var newString = text.replace(/\n/g, '');
// print new string
$(".section-overview textarea").val(newString);
},100);
}
});
});
<span lang="en-US">
)时,我就会收到一封电子邮件,并手动进行更正。