如何使用 JQuery 向 CKEditor 添加数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2108354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:47:38  来源:igfitidea点击:

How to add data to CKEditor using JQuery

jqueryckeditor

提问by Elitmiar

Everytime a page loads I need to load text into the CK Editor using JQuery, in order to get data from CK Editor I use

每次加载页面时,我都需要使用 JQuery 将文本加载到 CK 编辑器中,以便从我使用的 CK 编辑器获取数据

var editor_data = CKEDITOR.instances['editor1'].getData();

now is there a similar function I could use to put the data back into the editor?

现在是否有类似的功能可以用来将数据放回编辑器中?

I'm using ajax to set the data like this

我正在使用ajax来设置这样的数据

$.ajax({
  type: "POST",
  url: "/inc/ajax/basic.php?menu_id="+menu_id+"&info=3",
  success: function(msg){

    CKEDITOR.instances['editor1'].setData(msg);
  }
});

What am I doing wrong

我究竟做错了什么

回答by PanJanek

Try this:

尝试这个:

CKEDITOR.instances['editor1'].setData(html)

Where 'html' is a string containing content to edit.

其中 'html' 是包含要编辑的内容的字符串。

回答by mac786751

Because its not an array then just replace the instance like this

因为它不是数组,所以只需像这样替换实例

CKEDITOR.instances.editor1.setData(html)

回答by Happy Patel

CKEDITOR.instances['<%=ckEditor.ClientID%>'].setData(value);

回答by Vaibhav Jain

you should use data, and method for sending query string like this:

您应该使用数据和方法来发送这样的查询字符串:

$(document).ready(function()
{
  var querystring="menu_id="+menu_id+"&info=3";
  $.ajax({
  method: "POST",
  url: "/inc/ajax/basic.php",
  data:querystring,
  success: function(msg)
   {
     CKEDITOR.instances['editor1'].setData(msg);
   }
  });
});

回答by KHALID

var jqxhr = $.get( "file.php", function(data) {
CKEDITOR.instances.idOftextAreaName.setData( data );
    alert( "success" );
  })
.done(function() {
    //alert( "second success" );
})
.fail(function() {
    alert( "error" );
})
.always(function() {
   // alert( "finished" );
});

回答by Bhupesh

var editor = CKEDITOR.instances.help_ldesc;         
editor.setData(''); 
$.ajax({
url: urlstr, // Url to which the request is send
type: "POST",             // Type of request to be send, called as method
data:{action:"ex_form"}, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false,       // The content type used when sending data to the server.
cache:false,             // To unable request pages to be cached
processData:false,        // To send DOMDocument or non processed data file it is set to false
success: function(data)   // A function to be called if request succeeds
{
    //alert(data);
    var data1=data.split("~`");
    $('#help_id').val(data1[0]);
    $('#help_title').val(data1[1]);
    $('#help_sdesc').val(data1[2]);                 

    editor.setData(''+data1[3]);    
    var edata = editor.getData();
    alert(edata);
}
});

Use this code its works for me and (help_ldesc) is my textarea name.

使用此代码对我有用, (help_ldesc) 是我的 textarea 名称。

回答by liridons

From my experience using inside a function sometimes doesn't work properly. I'll suggest to use in:

根据我在函数内部使用的经验,有时无法正常工作。我建议用于:

    $(document).ready(function () {
    ...
    // instance, using default configuration.
    CKEDITOR.replace('editor1');
    //set data
    CKEDITOR.instances['editor1'].setData(data);
    ...
    });