TinyMCE的计数器无法正常工作

我知道那里有很多解决方案,但无法找到正确的解决方案。我已经在tinyMCE版本3中为自定义计数器编写了代码,该版本的maxlength属性不起作用。我想在计数器达到0时停止提供更多文本setcontent(""),substring(0,maxcount)这似乎是有问题的,因为当我在其修剪后两个字符之间输入任意2个字符时,这种方式不应该这样。我也尝试过使用evt.preventDefault()它的阻止功能,但无法再次输入IN进行击键,而击键也排除了bacspace和delete,但它无法正常工作。这是我的代码。


    tinyMCE.init({

        mode: "textareas",

        theme: "advanced",

        editor_selector: "mceEditor",

        paste_auto_cleanup_on_paste: 'true',

        theme_advanced_disable: 'justifyleft,justifyright,justifyfull,justifycenter,indent,image,anchor,sub,sup,unlink,outdent,help,removeformat,link,fontselect,hr,styleselect,formatselect,charmap,separator,code,visualaid,strikethrough,fullscreen',

        theme_advanced_buttons1: 'bold,italic,underline,numlist,bullist,undo,redo,cleanup,spellchecker',

        theme_advanced_buttons2: "",

        theme_advanced_buttons3: "",

        plugins: 'spellchecker,fullscreen,paste',

        spellchecker_languages: '+English=en-us',

        spellchecker_rpc_url: '<%out.print(request.getContextPath());%>/jazzy-spellchecker',

        theme_advanced_statusbar_location: "bottom",

        theme_advanced_path : false,

        statusbar: true,

        setup: function(editor)

        {

             editor.onKeyUp.add(function(evt)

            {

                var maxLengthRichTextArea = 5;

                 var inputRichTextArea = $(editor.getBody()).text();

                 var inputRichTextAreaLength = inputRichTextArea.length;

                 var value = maxLengthRichTextArea-inputRichTextAreaLength;

                 if(value >= 0)

                 {  

                 $(tinyMCE.activeEditor.getContainer()).find("#"+editor.id+"_path_row").html("Remaining chars: "+(value));

                }                  

            });

        }

    });


</script>

的HTML


<textarea id="450225" class="mceEditor"  maxlength="10" style="display: none;"></textarea>


catspeake
浏览 173回答 1
1回答

HUWWW

这是您要实现的目标的一个有效示例:HTML:<textarea id="text"></textarea>Javascript:tinymce.init({&nbsp; selector: "textarea#text",&nbsp; height: 500,&nbsp; menubar: false,&nbsp; setup: function(editor) {&nbsp; &nbsp; var maxlength = 5;&nbsp; &nbsp; var allowedKeys = [8, 37, 38, 39, 40];&nbsp; &nbsp; editor.on("keydown", function(e) {&nbsp; &nbsp; &nbsp; var count = $(editor.getBody()).text().length;&nbsp; &nbsp; &nbsp; if(allowedKeys.indexOf(e.which) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (count >= maxlength) {&nbsp; &nbsp; &nbsp; &nbsp; e.stopPropagation();&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; }});和Codepen,希望对您有所帮助!在我的代码中,最大长度为5,但是您可以通过var进行更改maxlength。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript