百度UEditor编译器中获取HTML内容和纯文本,设置UEditor编辑器的内容

获取纯文本内容的方法:

1.editor.getContentTxt();

获取编辑器中的纯文本内容,没有段落格式

javascript代码:示例

1
2
 //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
 console.log(editor.getContentTxt()); //输出:"12

2.editor.getPlainTxt();

得到编辑器的纯文本内容,但会保留段落格式

javascript代码:示例

1
2
 //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
 console.log(editor.getPlainTxt()); //输出:"1\n2\n

获取获取html的方法:

 editor.getContent();

javascript代码:示例

1
2
 //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
 var content = editor.getContent();
 //   返回值:<p>1<strong>2<em>34</em>5</strong>6</p>


设置编辑器的内容

setContent(String html)

设置编辑器的内容,可修改编辑器当前的html内容

  • 警告: 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  • 警告: 该方法会触发selectionchange事件

参数列表

参数名 类型 描述
html String 要插入的html内容

示例

javascript代码:

1
 editor.setContent('<p>test</p>');

2.setContent(String html, Boolean isAppendTo)

设置编辑器的内容,可修改编辑器当前的html内容

 参数列表

参数名 类型 描述
html String 要插入的html内容
isAppendTo Boolean 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入

示例

javascript代码:

1
2
 //假设设置前的编辑器内容是 <p>old text</p>
 editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>

官网说明https://ueditor.baidu.com/doc/#UE.Editor:getContent() 

猜你喜欢

转载自blog.csdn.net/luoyu6/article/details/83279954