javascript 如何从 CKEditor 获取 html?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7018753/
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
how to get html from CKEditor?
提问by Steven Zack
I'm using CKEditor in my web app, but I don't know how to get html content from it.http://cksource.com/ckeditor I searched online found one said using getData() method, but there is no getData() method after typing dot after the controler. Can anyone give me a sample code to get html from CKEditor controller? Thank you in advance.
我在我的网络应用程序中使用 CKEditor,但我不知道如何从中获取 html 内容。 http://cksource.com/ckeditor 我在网上搜索发现一个说使用 getData() 方法,但没有 getData () 方法在控制器后键入点。谁能给我一个示例代码来从 CKEditor 控制器获取 html?先感谢您。
回答by Tomasz Dzi?cielewski
To get htmlData from editor you should use the code snippet bellow:
要从编辑器获取 htmlData,您应该使用以下代码片段:
var htmldata = CKEDITOR.instances.Editor.document.getBody().getHtml();
If this solution won't work, check if you have BBCode
plugins installed.
如果此解决方案不起作用,请检查您是否BBCode
安装了插件。
回答by AlfonsoML
getData() is part of the javascript API. It seems that you are trying to do it at the server side, so you should check the specific API of whatever wrapper you are using, or just check the value in the form posted data.
getData() 是 javascript API 的一部分。似乎您正在尝试在服务器端执行此操作,因此您应该检查正在使用的任何包装器的特定 API,或者仅检查已发布数据的表单中的值。
回答by codewaggle
Not sure how you're implementing usage of the CKEditor.
不确定您如何实现 CKEditor 的使用。
If you're replacing a textarea using CKEDITOR.replace( 'NameOfTextarea', this should work:
如果您使用 CKEDITOR.replace('NameOfTextarea' 替换 textarea,这应该可以工作:
CKEDITOR.instances.NameOfTextarea.on( 'instanceReady', function( instanceReadyEventObj )
{
var editorInstanceData = CKEDITOR.instances.NameOfTextarea.getData();
alert( editorInstanceData );
});
Replace "NameOfTextarea" with the name of your textarea, it's used to name the editor instance.
将“NameOfTextarea”替换为您的文本区域的名称,它用于命名编辑器实例。
It's a good idea to put it inside the "on instanceReady" function so you don't get an undefined error.
将它放在“on instanceReady”函数中是个好主意,这样就不会出现未定义的错误。
Joe
乔