Insert text at the cursor position to a CKEditor using jQuery

只谈情不闲聊 提交于 2019-12-03 05:23:20

You should use this

$.fn.insertAtCaret = function (myValue) {
    myValue = myValue.trim();
    CKEDITOR.instances['idofeditor'].insertText(myValue);
};

CKEditor itself has a mechanism to insert text. If you update the textarea directly you are in effect bypassing some of the mechanisms CKEditor has to keep track of what text has been entered. Try this:

CKEDITOR.instances.IDofEditor.insertText('some text here');

More information here

I thought I should mention that if you are using the jQuery adapter for ckeditor you can insert text with jQuery this way which looks a little cleaner.

$('textarea#id_body').ckeditor().editor.insertText('some text here');

or if you are inserting HTML

$('textarea#id_body').ckeditor().editor.insertHtml('<a href="#">text</a>');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!