Javascript 如何在 CKEditor 中将文本附加到 html 源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5064358/
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-23 15:29:13  来源:igfitidea点击:

How can I append text to html source in CKEditor?

javascripthtmlckeditorfckeditor

提问by Alex Pliutau


I use CKEditor in my web-application. By click on one link i appends some text to CKEditor. It works fine. But when I open sourcetab, i can not append this text to the existing source. Can you help me how can I do it? Thank you in advance. Sorry for my english.


我在我的网络应用程序中使用 CKEditor。通过单击一个链接,我将一些文本附加到 CKEditor。它工作正常。但是当我打开source选项卡时,我无法将此文本附加到现有源中。你能帮我怎么做吗?先感谢您。对不起我的英语不好。

回答by Maricel

If you are trying to append HTML text, you could use the createFromHtml method like this for example:

如果您尝试附加 HTML 文本,您可以使用 createFromHtml 方法,例如:

var imgHtml = CKEDITOR.dom.element.createFromHtml("<img src=" + imageSrcUrl + " alt='' align='right'/>");

where imageSrcUrl is the image location and then you can insert it into the ckeditor source like this:

其中 imageSrcUrl 是图像位置,然后您可以像这样将其插入 ckeditor 源:

CKEDITOR.instances.body.insertElement(imgHtml);

There are other methods like insertHtml or insertText, you can check the CKEditor APIsfor more details on these.

还有其他方法,如 insertHtml 或 insertText,您可以查看CKEditor API以获取有关这些的更多详细信息。

回答by Webby

According to this post http://www.techsirius.com/2013/09/dynamically-insert-string-into-ckeditor.html

根据这篇文章 http://www.techsirius.com/2013/09/dynamically-insert-string-into-ckeditor.html

You can insert text into ckeditor(textarea). You just need to give an unique ID to ckeditor(textarea) after that follow below code.

您可以将文本插入 ckeditor(textarea)。在遵循以下代码之后,您只需要为 ckeditor(textarea) 提供一个唯一的 ID。

<script type=”text/javascript”>
  function insertIntoCkeditor(str){
    CKEDITOR.instances[ckeditor_id].insertText(str);
  }
</script>

This is working demo link. http://demo.techsirius.com/demo/dynamically-insert-string-into-ckeditor

这是工作演示链接。 http://demo.techsirius.com/demo/dynamically-insert-string-into-ckeditor

回答by alpc

Other sample working function :

其他示例工作功能:


  function insertIntoCkeditor(str,url){

  var tagHtml = ''+str+'';

    //CKEDITOR.instances['bilgi'].insertText(tagHtml);
    CKEDITOR.instances['bilgi'].insertHtml(tagHtml);
    //CKEDITOR.instances.body.insertElement(tagHtml);
  }

onclick="insertIntoCkeditor('Parakazan','Http://www.parakazan.org')"> 


回答by Micha?l Witrant

To append HTML at the endyou can do this:

在最后附加 HTML 您可以执行以下操作:

var targetEditor = CKEDITOR.instances.idOfYourTextarea;
var range = targetEditor.createRange();
range.moveToElementEditEnd(range.root);
targetEditor.insertHtml("<p>foo</p>", 'html', range);