Ckeditor character limitation with charcount plugin

[亡魂溺海] 提交于 2019-12-03 17:04:09
jazZRo

I used the ckeditor jQuery adapter for this.

<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:

<script src="/path-to/ckeditor/adapters/jquery.js"></script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!