<p>最近碰到一个采集的项目,需要把抓取的内容进行筛选过滤然后编辑入库,其中抓取的内容进行编辑会涉及到一个问题就是,把获取到的html数据回显到编辑器中,在网上找了到了些方法,但是不是很理想,最后自己查官网API才解决;</p><p>添加HTML内容到百度UEditor编译器中,先直接上代码:</p><pre class="brush:js;toolbar:false">utils.ajax("capture",{source_url:url},function(result){ if ( !result.code ) { return bootbox.alert({size: "small",message: result.msg}); } return spop({ template: result.msg.tips, autoclose: 3000, position : 'top-center', style: 'success', onClose: function () { $('input[name=name]').val(result.msg.data.title); $('input[name=en_name]').val(result.msg.data.en_name); $('input[name=keywords]').val(result.msg.data.keywords); editor.execCommand('insertHtml', result.msg.data.content);//最关键的一步 } }); });</pre><p><strong>相关函数:</strong><strong>execCommand函数</strong></p><pre class="brush:js;toolbar:false">execCommand(String cmd, String html)</pre><p>插入html代码</p><hr/><p><strong>参数列表</strong><br/></p><table><tbody><tr class="firstRow"><td width="501" valign="top" style="word-break: break-all;">参数名</td><td width="501" valign="top" style="word-break: break-all;">类型</td><td width="501" valign="top" style="word-break: break-all;">描述</td></tr><tr><td width="501" valign="top" style="word-break: break-all;">cmd</td><td width="501" valign="top" style="word-break: break-all;">String</td><td width="501" valign="top" style="word-break: break-all;">命令字符串</td></tr><tr><td valign="top" colspan="1" rowspan="1" style="word-break: break-all;">html</td><td valign="top" colspan="1" rowspan="1" style="word-break: break-all;">String</td><td valign="top" colspan="1" rowspan="1" style="word-break: break-all;">插入的html字符串</td></tr></tbody></table><p><strong>相关链接</strong></p><p>execCommand方法的官网链接:<br/></p><p>http://ueditor.baidu.com/doc/#UE.Editor:execCommand(String)</p><hr/><p>最后官网API还有ready事件,专门可以针对UEditor加载完成后的处理;<br/></p><p>http://ueditor.baidu.com/doc/#UE.Editor:ready</p><p>官网上有两种ready的事件的处理方法:</p><p>第一种是ready监听:</p><pre class="brush:js;toolbar:false">editor.addListener( 'ready', function( editor ) { editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点 });</pre><p>第二种类似于jquery的$(document).ready{}一样,也是初始完成之后执行:</p><pre class="brush:js;toolbar:false">editor.ready( function( editor ) { editor.setContent('初始化完毕'); });</pre><p>这两种的效果是一样的。</p><p>注意:上述两种方法,官网上的demo有个错误,就是function(editor)中的editor不是Editor()对象,而下方</p><pre class="brush:js;toolbar:false">editor.execCommand('insertHtml', content_old);</pre><p>editor应该是Editor()对象,否则会报“无效属性execCommand”的错误!所以,我这里把function(editor)改为了function(edt),这样就不会出现变量名冲突;</p><p>以上就是关于“百度编辑器ueditor追加html内容”的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流。</p>