如何使用jQuery更改CKEditor文本?

9

我有一个带有CKEditor(bbCode插件)的文本域。

<textarea id="editor1" name="conteudo" class="form-control" rows="3" required></textarea>

这是我的CKEditor实例:
$( document ).ready( function() {
    $( 'textarea#editor1' ).ckeditor();
} );

我正在进行一个 JSON 请求,该请求需要一个值,并且我希望这个值能够在这个 textarea 中被修改。我尝试使用 jQuery,但没有成功!以下是我的尝试:

video_id = "lLi1Lx2xTKI";

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
    description = data.data.description;
    // Attempt here
    $("#editor1").html(description);
});

更新

我尝试使用'.val()'但没有成功!


尝试过 $( 'textarea#editor1' ).val(description) 吗? - Wilmer
是的,@Wilmer。没有起作用。 - user2297392
1
没有必要使用jQuery... CKEDITOR.instances.editor1.setData("<p>HELLO</p>"); - epascarello
如果您想使用val(),则需要包含jquery适配器文件,请参阅http://docs.cksource.com/CKEditor_3.x/Developers_Guide/jQuery_Adapter。 - Wilmer
这是CKEditor 4的jQuery适配器指南 - Reinmar
2个回答

20

你不能简单地通过jQuery向CKEDITOR添加文本,相反应使用由CKEDITOR提供的api

CKEDITOR.instances.editor1.setData(data.data.description); 

你的代码看起来像这样

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){   
    CKEDITOR.instances.editor1.setData(data.data.description); 
});

Fiddle


(翻译:这是一个链接,可以访问http://jsfiddle.net/B4yGJ/43/)

2

尝试使用CKEditor的setData方法,而不是直接将描述写入文本区域。您可以在这里找到它的描述:

http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-popup

还要确保您的描述变量有值,我会使用一个临时alert(description);来进行确认,但您也可以使用JavaScript调试器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接