亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

TinyMCE的計數器無法正常工作

TinyMCE的計數器無法正常工作

catspeake 2021-05-10 12:14:30
我知道那里有很多解決方案,但無法找到正確的解決方案。我已經在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>
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

這是您要實現的目標的一個有效示例:


HTML:


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

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;

      }

    });

  }

});

和Codepen,希望對您有所幫助!在我的代碼中,最大長度為5,但是您可以通過var進行更改maxlength。


查看完整回答
反對 回復 2021-05-27
  • 1 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號