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()) } } }()