CKEditor的安裝與基本使用(JSP)

來源:互聯網
上載者:User

標籤: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代碼  
  1. <textarea class="ckeditor" cols="80" id="content" name="content" rows="10">  
  2.       CKEditor Test......(此處的內容會在編輯器中顯示)  
  3. </textarea>  
  4.   
  5. <script type="text/javascript">  
  6. //<![CDATA[  
  7.     CKEDITOR.replace(‘content‘,{toolbar:‘Full‘, skin : ‘kama‘});  
  8. //]]>  
  9. </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代碼  
  1. <script type="text/javascript">  
  2.     window.onload = function(){  
  3.         CKEDITOR.replace(‘content‘);  
  4.     }  
  5. </script>  

 

 

五、擷取編輯器裡的資料

      有時在提交表單時,須要推斷下編輯器裡的內容是否為空白。這時可使用CKEditor的JavaScript API:

      CKEDITOR.instances.content.getData()      

 

Java代碼  
  1. function test() {  
  2.     var editor_data = CKEDITOR.instances.content.getData();  
  3.     if(editor_data==null || editor_data==""){  
  4.         alert("請填寫內容!

    ");  

  5.         return false;  
  6.     }  
  7. }  

       注意:假設你在編輯器裡打了一堆的空格,內容是不為空白的,由於編輯器會把你打的空格都轉換成&nbsp;

 

至此,CKEdiotr編輯器就可以在頁面上顯示與使用。

 

補充說明:假設你的項目中使用struts2等架構,也可依據上面textarea的屬性name="content"來擷取編輯器裡的內容,最好使用BLOB來儲存。

CKEditor的安裝與基本使用(JSP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.