Fork me on GitHub

UEditor去除复制样式实现无格式粘贴

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

 

posted @ 2016-01-24 22:33  秋楓  阅读(17132)  评论(0编辑  收藏  举报