javascript CKEditor 字符数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5749002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CKEditor Character Count
提问by Flat Cat
First of all, this question WAS answered here: CKeditor character/word countbut all the answers give broken links... therefore it's not answered.
首先,这个问题在这里得到了回答:CKeditor character/word count但所有答案都给出了断开的链接......因此没有回答。
Using CKEditor, how to I get the character count on keyup, using jquery?
使用 CKEditor,如何使用 jquery 获取 keyup 的字符数?
Here's what shouldwork, but does notwork:
以下是应该有效但无效的方法:
<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>
I believe the problem lies in the "keyup" event. I don't get any response when referring to it like that... but I don't know what else to use.
我相信问题在于“keyup”事件。像这样提到它时我没有得到任何回应......但我不知道还能用什么。
回答by PsychoDUCK
You need to register an event on the CKEditr like so:
您需要像这样在 CKEditr 上注册一个事件:
editor.on( 'key', function( evt ){
updateCount( evt.editor.getData() );
}, editor.element.$ );
I also found this link that has a nice character count using jQuery and css. http://jsfiddle.net/VagrantRadio/2Jzpr/
我还发现这个链接使用 jQuery 和 css 有很好的字符数。 http://jsfiddle.net/VagrantRadio/2Jzpr/
回答by Sam Battat
Here is a quick solution with no plugins (not the best)
这是一个没有插件的快速解决方案(不是最好的)
var len = $("#cke_contents_message").children("iframe").contents().find("body").text().length;
[UPDATE] THE BETTER WAY TO DO THAT
[更新] 更好的方法
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);
});
});
回答by js1568
Try using .change()
instead of .keyup()
尝试使用.change()
代替.keyup()
回答by Sibu
Try this plugin, It worked for me http://www.n7studios.co.uk/2010/03/01/ckeditor-word-count-plugin/
试试这个插件,它对我有用 http://www.n7studios.co.uk/2010/03/01/ckeditor-word-count-plugin/