---
title: "获取ckeditor4.x里的值"
author: "麦田1990"
date: "2013-12-14T11:20:00Z"
tags: []
slug: "ckeditor4-x"
draft: false
lastmod: "2013-12-14T11:51:26Z"
id: 1989418
url: "https://alert-null.iteye.com/blog/1989418"
---

项目中有这样一个需求，使用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();
```