我使用的是 tinyMCE 版本 3,我使用的是富文本编辑器,它在输入时计算剩余的字符。由于 maxLength 属性不适用于 tinyMCE3。我已经以这种方式进行了硬编码,但它也计算了空白字符
< script type = "text/javascript" >
tinyMCE.init({
mode: "textareas",
theme: "advanced",
editor_selector: "mceEditor",
theme_advanced_statusbar_location: "bottom",
theme_advanced_path: false,
statusbar: true,
setup: function(editor) {
editor.onKeyUp.add(function(c) {
// var maxCount = document.getElementById(editor.id).maxLength;
var maxCount = 10;
console.log(editor.id);
var txt = $(editor.getBody()).text();
var txtLen = txt.length;
var value = maxCount - (txtLen);
$(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value));
if (txtLen > maxCount) {
editor.setContent("");
tinyMCE.activeEditor.selection.setContent(txt.substring(0, maxCount));
tinyMCE.activeEditor.focus();
}
});
}
}); <
/script>
<textarea id="1" class="mceEditor" cols="100" rows="7" wrap="" maxlength="10">test sample</textarea>
当我在中间输入 2 个字符时,它会删除最后两个字符,有什么方法可以在输入到达计数器“0”后停止输入。