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。
添加回答
舉報
0/150
提交
取消