javascript 带有charcount插件的Ckeditor字符限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27231626/
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 limitation with charcount plugin
提问by saimcan
How can i preventusers to enter new characters after max characterlimit is reached ?
达到最大字符数限制后,如何防止用户输入新字符?
Ckeditor charcount plugin just shows me the remaining characters, i want it to stop at 0. But it goes minus integers.
Ckeditor charcount 插件只显示剩余的字符,我希望它停在 0。但它会减去整数。
Here's my html code.
这是我的 html 代码。
<textarea id="myTextEditor1" name="myTextEditor"></textarea>
<script type="text/javascript">
CKEDITOR.replace('myTextEditor1', {
height: 200,
extraPlugins: 'charcount',
maxLength: 10,
toolbar: 'TinyBare',
toolbar_TinyBare: [
['Bold','Italic','Underline'],
['Undo','Redo'],['Cut','Copy','Paste'],
['NumberedList','BulletedList','Table'],['CharCount']
]
});
</script>
Do i have to use onChange plugin? If i have to how can i limit users entering new characters ?
我必须使用onChange 插件吗?如果我必须如何限制用户输入新字符?
回答by jazZRo
I used the ckeditor jQuery adapter for this.
我为此使用了 ckeditor jQuery 适配器。
<textarea id="myTextEditor1" name="myTextEditor"></textarea>
<script type="text/javascript">
$(function () {
var myEditor = $('#myTextEditor1');
myEditor.ckeditor({
height: 200,
extraPlugins: 'charcount',
maxLength: 10,
toolbar: 'TinyBare',
toolbar_TinyBare: [
['Bold','Italic','Underline'],
['Undo','Redo'],['Cut','Copy','Paste'],
['NumberedList','BulletedList','Table'],['CharCount']
]
}).ckeditor().editor.on('key', function(obj) {
if (obj.data.keyCode === 8 || obj.data.keyCode === 46) {
return true;
}
if (myEditor.ckeditor().editor.document.getBody().getText().length >= 10) {
alert('No more characters possible');
return false;
}else { return true; }
});
});
</script>
The keyCode check is to allow backspace and delete key presses. To use the jQuery adapter, don't forget to insert it:
keyCode 检查是为了允许退格和删除按键。要使用 jQuery 适配器,不要忘记插入它:
<script src="/path-to/ckeditor/adapters/jquery.js"></script>