Javascript CKEditor 插入 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27800375/
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
CKEditor Insert HTML
提问by saimcan
- I have a data from database.
- In my js file, I would like to change my CKEditor text editor's value.
- My value is raw html.
- I want this raw value to be written on an empty CKEditor text editor.
- 我有来自数据库的数据。
- 在我的 js 文件中,我想更改我的 CKEditor 文本编辑器的值。
- 我的价值是原始 html。
- 我希望将此原始值写入空的 CKEditor 文本编辑器。
I tried these but got an undefined function error all the time :
我试过这些,但一直有一个未定义的函数错误:
CKEDITOR.instances.myEditorID.insertHtml( '<p>This is a new paragraph.</p>' );
CKEDITOR.instances.myEditorID.setData( '<p>This is the editor data.</p>' );
I tried this too but still undefined function error :
我也试过这个,但仍然未定义的函数错误:
CKEDITOR.instances.YOUREDITORID.updateElement();
alert( document.getElementById( 'YOUREDITORID' ).value );
Instead of myEditorID i tried 'editor', 'editor1', 'editor2' but still doesn't work for me.
我尝试了“editor”、“editor1”、“editor2”,而不是 myEditorID,但仍然对我不起作用。
Thanks in advance.
提前致谢。
---Update---
- -更新 - -
This is the html of my ckeditor text editor :
这是我的 ckeditor 文本编辑器的 html:
<textarea id="myEditorID" name="myEditor"></textarea>
<script type="text/javascript">
$(function () {
var myEditor = $('#myEditorID');
myEditor.ckeditor({
height: 200,
extraPlugins: 'charcount',
maxLength: 2000,
toolbar: 'TinyBare',
toolbar_TinyBare: [
['Bold','Italic','Underline'],
['Undo','Redo'],['Cut','Copy','Paste'],
['NumberedList','BulletedList','Table'],['CharCount']
]
}).ckeditor().editor.on('key', function(obj) {
if (obj.data.keyCode === 8 || obj.data.keyCode === 46) {
return true;
}
if (myEditor.ckeditor().editor.document.getBody().getText().length >= 2000) {
alert('You have reached the maximum char length');
return false;
}
});
});
</script>
回答by roryok
Instead of myEditorID i tried 'editor', 'editor1', 'editor2' but still doesn't work for me.
我尝试了“editor”、“editor1”、“editor2”,而不是 myEditorID,但仍然对我不起作用。
You need to look at the HTML of your page and see what the ID field is for your editor. It will be something like this
您需要查看页面的 HTML 并查看编辑器的 ID 字段。它会是这样的
<textarea id="my_editor"></textarea>
That id attribute is what needs to go in here
该 id 属性是这里需要的
CKEDITOR.instances.my_editor.insertHtml('<p>This is a new paragraph.</p>');

