简体   繁体   English

TinyMCE的计数器没有按预期工作

[英]Counter for TinyMCE is not working as expected

I know there are so many solutions out there but cannot get right solution. 我知道那里有很多解决方案,但无法得到正确的解决方案。 I have written code for customized counter in tinyMCE version 3 which has maxlength attribute which is not working. 我在tinyMCE版本3中编写了自定义计数器的代码,其中maxlength属性不起作用。 I want to stop giving more text when counter reaches to 0, I have used setcontent("") and substring(0,maxcount) this seems to be problem because when I give any 2 characters in between its trimming last two charcters which should not be this way. 我想在计数器达到0时停止提供更多文本,我使用了setcontent("")substring(0,maxcount)这似乎是个问题,因为当我在它修剪最后两个字符之间给出任何2个字符时应该不会就是这样。 Also I have tried using evt.preventDefault() Its preventing but unable to type IN again for keydown and keypress also excluded bacspace and delete but its not working right. 此外,我尝试使用evt.preventDefault()它的阻止,但无法再次键入IN为keydown和keypress也排除bacspace和删除但它不能正常工作。 here is my code. 这是我的代码。

    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));
                }           
                if(inputRichTextAreaLength > maxLengthRichTextArea) {
                 editor.setContent("");
                tinyMCE.activeEditor.selection.setContent(inputRichTextArea.substring(0, maxLengthRichTextArea));
                }                  
            });
        }
    });

</script>

HTML HTML

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

This is working good 这很好用

But here when I add 6 its triimming of last digit 5 但是在这里我添加6它的最后一个数字5的修剪

How to solve this issue and max count is actually a higher number which comes from database. 如何解决这个问题,max count实际上是一个来自数据库的更高数字。

Here is a working example of what you are trying to achieve : 以下是您要实现的目标的实例:

HTML : HTML:

<textarea id="text"></textarea>

Javascript : Javascript:

tinymce.init({
  selector: "textarea#text",
  height: 500,
  menubar: false,
  setup: function(editor) {
    var maxlength = 5;
    var allowedKeys = [8, 37, 38, 39, 40];
    editor.on("keydown", function(e) {
      var count = $(editor.getBody()).text().length;
      if(allowedKeys.indexOf(e.which) != -1) {
         return;
      }
      if (count >= maxlength) {
        e.stopPropagation();
        return false;
      }
    });
  }
});

And the codepen , I hope that will help you ! codepen ,我希望这会对你有所帮助! In my code the max length is 5 , but you can change it via the var maxlength . 在我的代码中,最大长度为5 ,但您可以通过var maxlength更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM