CKEditor 入門

有多種方法能夠將CKEditor集成到你的頁面中,下面是最一般的作法。javascript

 

第一步:載入 CKEditorcss

http://ckeditor.com/download 下載ckeditor的最新版本(我下了個5月9號發佈的3.6),解壓後將 ckeditor 文件夾複製到web工程的根目錄下。在要使用CKEditor的頁面<head>塊中插入如下代碼,將其引入:html

 

Html代碼  收藏代碼java

  1. <head>  web

  2.     ...  數據庫

  3.     <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>  服務器

  4. </head>  jsp

 

 

第二部:建立 CKEditor 實例編輯器

 

首先,在頁面中插入一個<textarea>節點:工具

 

Html代碼  收藏代碼

  1. <textarea id="editor1" name="editor1">&lt;p&gt;Initial value.&lt;/p&gt;</textarea>  

 

若是你想讓編輯器在初始化時,顯示諸如從數據庫等處查出的數據,只須要將他們插入<textarea>節點中,如上面<p>節點中的內容。

 

建立好<textarea>後,用  CKEditor API 替換原來的HTML節點,以下:

 

Html代碼  收藏代碼

  1. <script type="text/javascript">  

  2.     CKEDITOR.replace( 'editor1' );  

  3. </script>  

 

或者在 window.onload 中:

 

Html代碼  收藏代碼

  1. <script type="text/javascript">  

  2.     window.onload = function()  

  3.     {  

  4.         CKEDITOR.replace( 'editor1' );  

  5.     };  

  6. </script>  

 

第三步:更改config.js

config.js是 CKEditor 的主配置文件,更改config.js來定製本身的 CKEditor 樣式:

 

Js代碼  收藏代碼

  1. /* 

  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. 

  3. For licensing, see LICENSE.html or http://ckeditor.com/license 

  4. */  

  5.   

  6. CKEDITOR.editorConfig = function( config )  

  7. {  

  8.     // Define changes to default configuration here. For example:  

  9.     // config.language = 'fr';  

  10.     // config.uiColor = '#AADC6E';  

  11.     config.language = 'zh-cn'// 配置語言  

  12.     config.uiColor = '#FFF'// 背景顏色  

  13.     config.width = 'auto'// 寬度  

  14.     config.height = '300px'// 高度  

  15.     config.skin = 'office2003';// 界面v2,kama,office2003  

  16.     config.toolbar = 'MyToolbar';// 工具欄風格(基礎'Basic'、全能'Full'、自定義)plugins/toolbar/plugin.js  

  17.     config.toolbarCanCollapse = false;// 工具欄是否能夠被收縮  

  18.     config.resize_enabled = false;// 取消 「拖拽以改變尺寸」功能 plugins/resize/plugin.js  

  19.       

  20.     //自定義的工具欄      

  21.     config.toolbar_MyToolbar =  

  22.     [  

  23.         ['Source','-','Save','Preview','Print'],  

  24.         ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Scayt'],  

  25.         ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],  

  26.         ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],  

  27.         '/',  

  28.         ['Styles','Format'],  

  29.         ['Bold','Italic','Strike'],  

  30.         ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],  

  31.         ['Link','Unlink','Anchor'],  

  32.         ['Maximize','-','About']  

  33.     ];  

  34. };  

 

 

第四步:提交編輯器內容

 

能夠將 CKEditor 實例看做一個<textarea>處理,在表單提交時, CKEditor 實例中的內容被提交到服務器,能夠經過<textarea> 的名稱得到其內容。

 

若是須要在客戶端得到 CKEditor 實例的內容,能夠經過調用 CKEditor API,在表單提交前對其進行處理,以下:

 

Html代碼  收藏代碼

  1. <script type="text/javascript">  

  2.     var editor_data = CKEDITOR.instances.editor1.getData();  

  3. </script>  

 

一個完整的例子:

Html代碼  收藏代碼

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  

  2. <%  

  3. String path = request.getContextPath();  

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  5. %>  

  6.   

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  8. <html>  

  9.   <head>  

  10.     <base href="<%=basePath%>">  

  11.       

  12.     <title>CKEditor</title>  

  13.     <meta http-equiv="pragma" content="no-cache">  

  14.     <meta http-equiv="cache-control" content="no-cache">  

  15.     <meta http-equiv="expires" content="0">      

  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

  17.     <meta http-equiv="description" content="This is my page">  

  18.     <!-- 

  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 

  20.     -->  

  21.     <script type="text/javascript" src="ckeditor/ckeditor.js"></script>  

  22.   </head>  

  23.     

  24.   <body>  

  25.         <form id="form1" name="form1" method="post" action="display.jsp">  

  26.             <table width="650" height="400" border="0" align="center">  

  27.                 <tr>  

  28.                     <td valign="top">  

  29.                         內容:  

  30.                     </td>  

  31.                     <td>  

  32.                         <textarea id="editor1" name="editor1">&lt;p&gt;Initial value.&lt;/p&gt;</textarea>  

  33.                         <script type="text/javascript">  

  34.                             CKEDITOR.replace( 'editor1' );  

  35.                         </script>  

  36.                     </td>  

  37.                 </tr>  

  38.                 <tr>  

  39.                     <td></td>  

  40.                     <td>  

  41.                         <input type="submit" name="Submit" value="提交" />  

  42.                         <input type="reset" name="Reset" value="重置" />  

  43.                     </td>  

  44.                 </tr>  

  45.             </table>  

  46.         </form>  

  47.     </body>  

  48. </html>  

 

顯示結果以下:


相關文章
相關標籤/搜索