我使用这个:
$(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);
});
body_doc是可编辑的iframe,如果您使用的是可编辑的DIV,你可以退出了.find(“主体”)部分。基本上,它检测粘贴事件,检查位置清理新文本,然后将清除的文本放回到粘贴的位置。 (听起来令人困惑......但它并不像听起来那么糟糕)
setTimeout是需要的,因为直到它实际上粘贴到元素中,才能获取文本,一旦粘贴开始就粘贴事件。
好运与此...从Word生成的内容(无论是在粘贴,并保存为HTML留下许多有待改进);-) – scunliffe 2010-05-20 15:10:29
我问或多或少同样的问题,然后在http://stackoverflow.com/questions/391291/how-do-i-remove-word-markup-crap-when-inserting-to-a-form,但你的标题更好。虽然,为什么限制自己的JavaScript和不考虑在服务器上做到这一点? – 2010-05-20 18:46:39