Javascript 编辑字符计数
首先,这里回答了这个问题:但是所有的答案都给出了断开的链接。。。因此它没有得到回答 使用CKEditor,如何使用jquery获得keyup上的字符数 以下是应该起作用,但不起作用的内容:Javascript 编辑字符计数,javascript,jquery,count,ckeditor,character,Javascript,Jquery,Count,Ckeditor,Character,首先,这里回答了这个问题:但是所有的答案都给出了断开的链接。。。因此它没有得到回答 使用CKEditor,如何使用jquery获得keyup上的字符数 以下是应该起作用,但不起作用的内容: <textarea id="newKnowledge_body"></textarea> <script> $(function(){ // this DOES work, fyi $("#newKnowledge_body").ckeditor(
<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>
$(函数(){
//这确实有效,仅供参考
$(“#newKnowledge_body”).ckeditor();
//这行不通
$(“#newKnowledge_body”).keyup(函数(){
var len=$(“#newKnowledge_body”).val().length;
警报(len);
});
});
我认为问题在于“钥匙”事件。当我这样提到它时,没有得到任何回应。。。但是我不知道还能用什么。试试使用
.change()
而不是.keyup()
试试这个插件,它对我有用http://www.n7studios.co.uk/2010/03/01/ckeditor-word-count-plugin/您需要在CKEDIT上注册一个事件,如下所示:
editor.on( 'key', function( evt ){
updateCount( evt.editor.getData() );
}, editor.element.$ );
我还发现这个链接使用jQuery和css具有很好的字符数。
这里有一个没有插件的快速解决方案(不是最好的) [更新]更好的方法是
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);
});
});
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);
});
});