UEditor內置了無格式粘貼的功能,只需要簡單的配置即可。
1、修改ueditor.config.js,開啓retainOnlyLabelPasted,並設置爲true
2、開啓pasteplain,並設爲true
3、開啓filterTxtRules過濾規則。
注意:filterTxtRules過濾規則默認只過濾p、div、li這幾個標籤。如果h1~h6這類標籤,只是將標籤體替換爲p標籤,並沒有將標籤體的樣式去除。因而通過itextpdf生成pdf時時往往會出現異常。因而,較爲完善的做法應該是將所有標籤的樣式都去掉(將過濾規則改爲{$:{}})。
修改後的過濾規則:
'filterTxtRules' : function(){
function transP(node){
node.tagName = 'p';
node.setStyle();
}
return {
//直接刪除及其字節點內容
'-' : 'script style object iframe embed input select',
'p': {$:{}},
'br':{$:{}},
'div':{$:{}},
'li':{$:{}},
'caption':{$:{}},
'th':{$:{}},
'tr':{$:{}},
'h1':{$:{}},'h2':{$:{}},'h3':{$:{}},'h4':{$:{}},'h5':{$:{}},'h6':{$:{}},
'td':function(node){
//沒有內容的td直接刪掉
var txt = !!node.innerText();
if(txt){
node.parentNode.insertAfter(UE.uNode.createText(' '),node);
}
node.parentNode.removeChild(node,node.innerText())
}
}
}()