【发布时间】:2019-05-06 17:31:09
【问题描述】:
我使用的是 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>
Counting in negative numbers and setting content to blank
当我在中间输入 2 个字符时,它会删除最后两个字符,有什么方法可以在达到计数器“0”后停止输入。
【问题讨论】:
-
使用
abs使其积极var value = Math.abs(maxCount-(txtLen)) -
@GetOffMyLawn 它只是删除负数并在 0 后显示为 1,2,3
标签: javascript jquery html string tinymce-3