標籤:local demo 提交表單 前端 padding color get normal
文章分類:Web前端
一、下載CKEditor
1. 直接。當前最新版本號碼為3.5:
http://download.cksource.com/CKEditor/CKEditor/CKEditor%203.5/ckeditor_3.5.zip
2. 或者可直接到官方網站下載:
http://ckeditor.com/download
二、安裝CKEditor
解壓ckeditor_3.5.zip,得到ckeditor目錄,將ckeditor整個目錄拷貝到項目project的根目錄下,即WebRoot下
三、驗證CKEditor是否成功安裝
部署執行項目,訪問: http://localhost/項目名稱/ckeditor/_samples/index.html
則可出現 “CKEditor Samples List” 的Demo頁面,說明CKeditor已成功安裝
四、應用CKEditor
1. 匯入JS檔案
<script type="text/javascript" src="<%=request.getContextPath()%>/ckeditor/ckeditor.js"></script>
2. 建立和使用CKEditor
Java代碼
- <textarea class="ckeditor" cols="80" id="content" name="content" rows="10">
- CKEditor Test......(此處的內容會在編輯器中顯示)
- </textarea>
-
- <script type="text/javascript">
- //<![CDATA[
- CKEDITOR.replace(‘content‘,{toolbar:‘Full‘, skin : ‘kama‘});
- //]]>
- </script>
說明:
1) textarea屬性值 name="content": 名字可任意定義。但必須與以下的CKEDITOR.replace(‘content‘);匹配
2) CKEDITOR.replace(‘content‘);是最主要的寫法。表示使用CKEditor的JavaScript API建立的編輯器執行個體,
替換上面的textarea。
若要為CKEditor添加一些屬性設定。則其屬性必須寫在{}花括弧內,
如上{toolbar:‘Full‘, skin : ‘kama‘},
詳細的屬性網上非常多文章均可搜尋到,這裡不累述了。
3) 上面的textarea後面的“CKEDITOR.replace(‘content‘);”指令碼也能夠寫在<head></head>標籤內
Java代碼
- <script type="text/javascript">
- window.onload = function(){
- CKEDITOR.replace(‘content‘);
- }
- </script>
五、擷取編輯器裡的資料
有時在提交表單時,須要推斷下編輯器裡的內容是否為空白。這時可使用CKEditor的JavaScript API:
CKEDITOR.instances.content.getData()
Java代碼
- function test() {
- var editor_data = CKEDITOR.instances.content.getData();
- if(editor_data==null || editor_data==""){
- alert("請填寫內容!
");
- return false;
- }
- }
注意:假設你在編輯器裡打了一堆的空格,內容是不為空白的,由於編輯器會把你打的空格都轉換成
至此,CKEdiotr編輯器就可以在頁面上顯示與使用。
補充說明:假設你的項目中使用struts2等架構,也可依據上面textarea的屬性name="content"來擷取編輯器裡的內容,最好使用BLOB來儲存。
CKEditor的安裝與基本使用(JSP)