gpt4 book ai didi

javascript - TinyMCE 的计数器未按预期工作

转载 作者:太空狗 更新时间:2023-10-29 14:56:57 24 4
gpt4 key购买 nike

我知道有很多解决方案,但找不到正确的解决方案。我已经在 tinyMCE 版本 3 中编写了自定义计数器的代码,该版本的 maxlength 属性不起作用。我想在计数器达到 0 时停止提供更多文本,我使用了 setcontent("")substring(0,maxcount) 这似乎是个问题,因为当我在其 trim 的最后两个字符之间给出任何 2 个字符,这不应该是这样的。我也尝试过使用 evt.preventDefault() 它可以防止但无法再次键入 keydown 和 keypress 也排除了 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));
}
if(inputRichTextAreaLength > maxLengthRichTextArea) {
editor.setContent("");
tinyMCE.activeEditor.selection.setContent(inputRichTextArea.substring(0, maxLengthRichTextArea));
}
});
}
});

</script>

HTML

<textarea id="450225" class="mceEditor"  maxlength="10" style="display: none;"></textarea>

This is working good

But here when I add 6 its triimming of last digit 5

如何解决这个问题,最大计数实际上是来自数据库的更高数字。

最佳答案

这是您要实现的目标的工作示例:

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 更改它。

关于javascript - TinyMCE 的计数器未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56082802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com