在线文本编辑器无法使用我的 javascript 代码

Online text editor not working with my javascript code

我有一个在线文本编辑器 "CKEditor" 通过引用它来工作,并为我希望编辑器使用的文本区域创建一个特定的 class 名称 "ckeditor",编辑器与 textarea 配合使用很好,但是一旦我通过 java 脚本函数添加 textarea,它就会显示没有编辑器的 textarea,这里是 java 脚本代码:

<script src="https://cdn.ckeditor.com/4.6.2/standard/ckeditor.js"></script>
<script>
function myFunction(){
    var target = document.getElementById("target"); 
    target.innerHTML = '<textarea name="editor1" class="ckeditor"></textarea>';
}
</script>

这是 html 代码:

<!--the editor works with this textarea-->
<textarea name="editor1" class="ckeditor"></textarea>

<button onClick="myFunction()">runFunction</button>
<!--the div I want java script to place the editor by clicking the above button-->
<div id="target">some text</div>

有什么解决办法吗?

来自the documentation

Use the CKEDITOR.replace() method to replace the existing <textarea> element with CKEditor.

示例代码显示的位置:

CKEDITOR.replace( 'editor1' )

其中 editor1 是文档中文本区域的 ID。

所以给文本区域一个ID,然后在将它添加到文档后调用该函数。