JS获取ckeditor4.x里的值

来源:互联网 发布:2016淘宝服装销售排行 编辑:程序博客网 时间:2024/05/16 02:27
项目中有这样一个需求,使用ckeditor可以上传图片,需要在前端验证一下不可上传多于5张图片。
以下是查看源代码所看到的ckeditor里的值
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <p>AAAAA</p>  
  2. <p><img alt="" src="/eHomePlus/img/uploadImg/f9d6663f-94f3-4ff5-b699-d17a5fbbd49d.png"  
  3. style="height:426px; width:447px" /></p>  
  4. <p>BBBB</p>  
  5. <p><img alt="" src="/eHomePlus/img/uploadImg/f15b9fb1-adbd-4495-ad76-59cc1e6b6344.png"  
  6. style="height:427px; width:455px" /></p>  
  7. <p>CCC</p>  

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

ckeditor提供了这样一个方法可以做到
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CKEDITOR.instances.editor1.getData()  

所以代码可写为:
[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. var content=CKEDITOR.instances.entity_content.getData();//entity_content为textarea的ID  
  2. var imgSize = $(content).find("img").size();  

另外也有其他方法可以取得编辑器内的值
[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. var editor;  
  2. $(function() {  
  3.     editor = CKEDITOR.replace('content');  
  4. })  
  5. editor.document.getBody().getText();//取得纯文本  
  6. editor.document.getBody().getHtml();//取得html文本  
0 0