Ckeditor註冊事件

這段時間使用js+cookies進行自動草稿保存,我的覺的,這些全在客戶端處理比較的好,因此沒有使用AJAX+數據庫的自動草稿保存方法。javascript

結果出現Ckeditor沒法綁定onkeyup,onselect,onclick事件的問題,查看了Ckeditor的API,發現以下說明:html

 

  1. instanceReady.ckeditor: fired when the editor is created, but before any callback being passed to the ckeditor() method.   
  2. setData.ckeditor: fired when data is set into the editor.   
  3. getData.ckeditor: fired when data is fetched from the editor. The current editor data is also passed in the arguments.   
  4. destroy.ckeditor: fired when the editor gets destroyed. It can be used, for example, to execute some cleanup on the page.   


估計是使用instanceReady,而後在網上查找了一翻,要找這東西的事件綁定,真難。找了N久,找到一個Ckeditor二次開發的例子,通過修改,終於成功。下面是綁定方法。java

 

首先是文本編輯器的綁定:數據庫

 

  1. <textarea name="Content" rows="10" cols="20" id="Content"></textarea>  
  2.   <script type="text/javascript">  
  3.   var editor = CKEDITOR.replace('Content', {  
  4.   language: 'zh-cn', //簡體中文    
  5.   width: 739,  
  6.   height: 125,  
  7.   toolbar: 'DJArticle'//工具欄的名稱  
  8.   });    
  9.   </script>  


這樣,編輯器就出現了。而後是綁定onkeyup,onselect,onclick事件。cookie

 

  1. <script type="text/javascript">  
  2.   
  3.     CKEDITOR.instances["Content"].on("instanceReady", function () {  
  4.         //set keyup event  
  5.         this.document.on("keyup", AutoSave);  
  6.         //and click event  
  7.         this.document.on("click", AutoSave);  
  8.         //and select event  
  9.         this.document.on("select", AutoSave);  
  10.     });  
  11.   
  12.   
  13.     function AutoSave() {//相應的操做過程,能夠按下面寫,也能夠按通常javascript過程寫。  
  14.         CKEDITOR.tools.setTimeout(function () {  
  15.             alert("10101010");  
  16.         }, 0);  
  17.     }     
  18.   
  19.             </script>  


至此,綁定完成!編輯器

相關文章
相關標籤/搜索