字符限制以 "negative" 数字显示剩余字符
Character Limit is showing remaining characters in "negative" numbers
我使用的是 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
Counting in negative numbers and setting content to blank
当我在删除最后两个字符之间输入 2 个字符时,有什么方法可以在它到达计数器“0”后停止提供输入。
我不确定我是否完全理解您的问题,因为这里发生了很多事情。
将剩余字符显示为负数
我看到,在您的代码中,如果您键入 11 个字符,第 11 个字符将被删除,但剩余的字符将显示“-1”而不是“0”。这是你的抱怨吗?之所以会出现这种情况是因为你trim编辑器的文本内容下到maxCount,但是你剩余的chars值是在trim出现之前计算的,所以它仍然有maxCount的值 - un trim中等长度。您可以通过多种方式轻松解决此问题,包括更改此:
$(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value));
对此:
$(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value > 0 ? value : 0));
计算空白字符数
如果您不想 任何 个空白字符包含在计数中,请使用:
var txtLen = txt.replace(/\s/g,"").length;
如果您想要 trim 只是结尾处的空白字符,请使用:
var txtLen = txt.trim().length;
我使用的是 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
Counting in negative numbers and setting content to blank
当我在删除最后两个字符之间输入 2 个字符时,有什么方法可以在它到达计数器“0”后停止提供输入。
我不确定我是否完全理解您的问题,因为这里发生了很多事情。
将剩余字符显示为负数
我看到,在您的代码中,如果您键入 11 个字符,第 11 个字符将被删除,但剩余的字符将显示“-1”而不是“0”。这是你的抱怨吗?之所以会出现这种情况是因为你trim编辑器的文本内容下到maxCount,但是你剩余的chars值是在trim出现之前计算的,所以它仍然有maxCount的值 - un trim中等长度。您可以通过多种方式轻松解决此问题,包括更改此:
$(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value));
对此:
$(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value > 0 ? value : 0));
计算空白字符数
如果您不想 任何 个空白字符包含在计数中,请使用:
var txtLen = txt.replace(/\s/g,"").length;
如果您想要 trim 只是结尾处的空白字符,请使用:
var txtLen = txt.trim().length;