1. 程式人生 > >百度編輯器Ueditor 初始化載入內容失敗解決辦法

百度編輯器Ueditor 初始化載入內容失敗解決辦法

專案上有用到百度文字編輯器ueditor,在頁面載入的時候初始化編輯器內容時候,使用

$.document.ready(function()
{
  UE.getEditor('editor').setContent('歡迎光臨');
})

setContent方法無法載入內容,提示編輯器為空,後來想想,可能是編輯器還沒有載入完就執行此指令碼導致的。後在網上找資料,可以判斷ueditor編輯器完成載入後再載入內容:

核心內容如下

var editor_a = new baidu.editor.ui.Editor(editorOption);
    editor_a.render('myEditor'
); editor_a.ready(function() { editor_a.setContent($('#content').val()); });

具體例子:

$(document).ready(function () {
    // 自定義的編輯器配置項,此處定義的配置項將覆蓋editor_config.js中的同名配置
    var editorOption = {
        //這裡可以選擇自己需要的工具按鈕名稱,此處僅選擇如下五個
       toolbars:[['fullscreen', 'source', '|', 'undo', 'redo'
, '|', 'bold', 'italic', 'underline', 'removeformat','|', 'forecolor', 'backcolor', '|', 'fontfamily', 'fontsize', '|','justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|','link', 'unlink', '|','simpleupload'
, 'insertimage', 'insertvideo', 'music', 'attachment']], //focus時自動清空初始化時的內容 autoClearinitialContent: true, //關閉elementPath elementPathEnabled: false }; var editor_a = new baidu.editor.ui.Editor(editorOption); editor_a.render('myEditor'); editor_a.ready(function() { editor_a.setContent($('#content').val()); //賦值給UEditor }); $('#title').val(title); //title賦值 });