首先,这个问题在这里得到了回答:CKeditor character/word count但所有答案都给出了损坏的链接...因此没有得到解答。
使用 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”事件上。当我这样引用它时没有得到任何回应...但我不知道还能使用什么。
最佳答案
这是一个无需插件的快速解决方案(不是最好的)
var len = $("#cke_contents_message").children("iframe").contents().find("body").text().length;
[更新]更好的方法
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);
});
});
关于javascript - CKEditor 字符计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5749002/