当 textarea 被初始化为 ckeditor 时,我们如何填充 textarea 值

How can we populate textarea value when textarea is initilized as ckeditor

好吧,我一直在使用 HTML FORMS 开发 CKEDITOR,后端是 asp.net core v3.1。我在呈现和绑定 html 表单时使用了 asp 标签助手。

代码如下:

 <div class="form-group">
                                <label asp-for="Content" class="control-label"></label>
                                <div class="input-group">
                                
                                    <textarea asp-for="Content" class="form-control" required placeholder="Content"></textarea>
                                    <span asp-validation-for="Content" class="text-danger"></span>
                                </div>
                            </div>

我有两个页面创建和编辑,分别创建数据库的第一个表单条目并更新值。

所以当我加载编辑页面时,所有的值都被加载但是 CKEditor 值在它绑定到 textarea 之后没有被加载。

值未显示在 HTML CKEDITOR 内容区域。

CKEDITOR INITILIZE CODE IS BELOW

  <script>
        // Replace the <textarea id="editor1"> with a CKEditor
        // instance, using default configuration.
        $(function () {
         CKEDITOR.replace('Content');        
        })
       
    </script>
}

API 文档记录了这方面的方法:

  1. 设置数据

    setData.
CKEDITOR.instances.editor1.setData( '<p>This is the editor data.</p>' );
  1. 插入元素

    insertElement.
var element = CKEDITOR.dom.element.createFromHtml( '<img src="hello.png" border="0" title="Hello" />' );
CKEDITOR.instances.editor1.insertElement( element );
  1. 插入文本

    insertText.
CKEDITOR.instances.editor1.insertText( ' line1 \n\n line2' );
  1. 插入Html

    insertHtml.

CKEDITOR.instances.editor1.insertHtml( '<p>This is a new paragraph.</p>' );

我认为你需要的是 setData 因为其他方法在光标位置附加 text/html。