JS获取ckeditor4.x里的值

项目中有这样一个需求,使用ckeditor可以上传图片,需要在前端验证一下不可上传多于5张图片。
以下是查看源代码所看到的ckeditor里的值
<p>AAAAA</p>
<p><img alt="" src="/eHomePlus/img/uploadImg/f9d6663f-94f3-4ff5-b699-d17a5fbbd49d.png"
style="height:426px; width:447px" /></p>
<p>BBBB</p>
<p><img alt="" src="/eHomePlus/img/uploadImg/f15b9fb1-adbd-4495-ad76-59cc1e6b6344.png"
style="height:427px; width:455px" /></p>
<p>CCC</p>

要想知道有多少张图片,通过img标签的个数可以看出。那首先就需要获取到ckeditor里的值

ckeditor提供了这样一个方法可以做到
CKEDITOR.instances.editor1.getData()

所以代码可写为:
var content=CKEDITOR.instances.entity_content.getData();//entity_content为textarea的ID
var imgSize = $(content).find("img").size();

另外也有其他方法可以取得编辑器内的值
var editor;
$(function() {
	editor = CKEDITOR.replace('content');
})
editor.document.getBody().getText();//取得纯文本
editor.document.getBody().getHtml();//取得html文本


posted on 2013-12-14 11:20  itmyhome  阅读(1207)  评论(0编辑  收藏  举报

导航