简体   繁体   English

CKEditor-在setData之后insertText不起作用

[英]CKEditor - insertText not working after setData

I try to insert something after clear the content by using setData(""), then I insert something to it using insertText method. 我尝试通过使用setData(“”)清除内容后插入一些内容,然后使用insertText方法向其中插入一些内容。 But the content remains empty. 但是内容仍然为空。

Relevant code: 相关代码:

CKEDITOR.instances.content.setData("");
CKEDITOR.instances.content.focus();
CKEDITOR.instances.content.insertText("the text I want to insert");

The code above is not working. 上面的代码不起作用。 Anyone could help me? 有人可以帮助我吗? finding another way to clear the content in CKEditor rather than using setData("") or make the insertText method working after setData would both help solve my problem. 寻找另一种方法来清除CKEditor中的内容,而不是使用setData(“”)或在setData之后使insertText方法起作用,都将有助于解决我的问题。 Many thanks! 非常感谢!

I've got a same problem but the point was that setData() method is asynchronous. 我遇到了同样的问题,但关键是setData()方法是异步的。 If you want to run something after setData() , you have to create a callback method . 如果要在setData()之后运行某些内容,则必须创建一个回调方法

var fooCallback = function(){
    CKEDITOR.instances.content.focus();
    CKEDITOR.instances.content.insertText("the text I want to insert");
};
CKEDITOR.instances.content.setData("", fooCallback);

Works for me neither. 都不适合我。 Must be a bug. 必须是一个错误。 But with jquery there is a way arround. 但是使用jQuery,有一种方法。

$('#content').val('the text I want to insert');

For the jquery adapter have a look here: http://ckeditor.com/blog/CKEditor_for_jQuery 对于jquery适配器,请在这里查看: http : //ckeditor.com/blog/CKEditor_for_jQuery

I encountered the same issues and found a solution. 我遇到了同样的问题,并找到了解决方案。 When "clearing" your editor, do not use an empty string, instead, use CKEDITOR.yourEditor.setData('<span></span>') , or (probably) some other equivalent "valid html" that displays nothing. “清除”编辑器时,请勿使用空字符串,而应使用CKEDITOR.yourEditor.setData('<span></span>')或(可能)其他等效的“有效html”,其中不显示任何内容。 I have only tried this with the spans for now. 我现在只尝试了跨度。 subsequent calls to the setData method will not fail. 随后对setData方法的调用不会失败。 Enjoy. 请享用。

I had the same problem but installing the jquery adapter as suggested by the accepted solution is not an option for me. 我遇到了同样的问题,但是按照我接受的解决方案的建议安装jquery适配器不是我的选择。

I was able to make it work with the following workaround on the second call: 我能够在第二个呼叫中使用以下变通办法来工作:

setTimeout(function(){
  CKEDITOR.instances["myEditor"].setData(newText);
}, 0);

I got the idea from here: https://dev.ckeditor.com/ticket/10663#comment:7 我从这里得到了这个主意: https : //dev.ckeditor.com/ticket/10663#comment:7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM