2011-04-21 22 views
2

首先,這個問題是在這裏回答:CKeditor character/word count但所有的答案給斷開的鏈接......因此它沒有回答。CKEditor字符數

使用CKEditor,如何使用jquery獲取keyup上的字符數?

這裏是應該工作,但確實工作:

<textarea id="newKnowledge_body"></textarea> 
<script> 
$(function(){ 
     // this DOES work, fyi 
     $("#newKnowledge_body").ckeditor(); 

     // this DOES NOT work 
     $("#newKnowledge_body").keyup(function(){ 
      var len = $("#newKnowledge_body").val().length; 
      alert(len); 
     }); 
}); 
</script> 

我認爲,問題就出在 「KEYUP」 事件。我沒有得到任何迴應時,像這樣......但我不知道還有什麼其他用途。

回答

0

嘗試使用.change(),而不是.keyup()

0

試試這個插件,它爲我http://www.n7studios.co.uk/2010/03/01/ckeditor-word-count-plugin/

1

你需要像這樣的CKEditr註冊事件:

editor.on('key', function(evt){ 
    updateCount(evt.editor.getData());  
}, editor.element.$); 

我也發現了這個鏈接,有使用jQuery和CSS一個不錯的字符數。 http://jsfiddle.net/VagrantRadio/2Jzpr/

1

這裏是無插件的快速溶液(不是最好的)

var len = $("#cke_contents_message").children("iframe").contents().find("body").text().length; 

[UPDATE]更好的方式來做到這一點

var editor = $('YOUR_TEXT_AREA').ckeditorGet(); 
editor.on("instanceReady", function(){      
    this.document.on("keyup", function(){ 
     var editorContent = $(editor.getData()); 
     var plainEditorContent = editorContent.text().trim(); 
     console.log(plainEditorContent); 
     console.log("Length: "+plainEditorContent.length); 
    }); 
});