gpt4 book ai didi

javascript - TinyMCE 文本编辑器最大字符限制

转载 作者:可可西里 更新时间:2023-11-01 02:23:20 26 4
gpt4 key购买 nike

我正在为 <textarea> 使用 TinyMCE .我的要求是将字符大小限制为 2000,并在工具栏下方某处显示剩余字符。我以某种方式设法获得了字符编号;现在我只能显示剩余的字符并防止超出限制。

这是我的 TinyMCE 代码

tinyMCE.init({
// General options
mode : "textareas",
theme : "simple",
plugins : "autolink,lists,pagebreak,style,table,save,advhr,advimage,
advlink,emotions,media,noneditable,visualchars,nonbreaking,
xhtmlxtras,template",

// Theme options
theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,
justifyleft,justifycenter,justifyright,
justifyfull,|,styleselect,formatselect,
fontselect,fontsizeselect",
theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,undo,redo,|,
link,unlink,anchor,image,code,|,forecolor,
backcolor",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
charLimit : 10, // this is a default value which can get modified later
setup : function(ed) {
//peform this action every time a key is pressed
ed.onKeyUp.add(function(ed, e) {
//define local variables
var tinymax, tinylen, htmlcount;
//manually setting our max character limit
tinymax = ed.settings.charLimit;
//grabbing the length of the curent editors content
tinylen = ed.getContent().replace(/(<([^>]+)>)/ig,"").length;
//setting up the text string that will display in the path area
htmlcount = "HTML Character Count: " + tinylen + "/" + tinymax;
//if the user has exceeded the max turn the path bar red.
if (tinylen>tinymax){


}
});
}
});

出于测试目的,我试图限制最多 10 个字符。
欢迎提出任何建议。

最佳答案

我建议你在 KeyDown 上执行你的代码,因为在 KeyUp 上字母已经在编辑器中了。

    //peform this action every time a key is pressed
ed.onKeyDown.add(function(ed, e) {

//define local variables
var tinymax, tinylen, htmlcount;

//manually setting our max character limit
tinymax = ed.settings.charLimit;

//grabbing the length of the curent editors content
tinylen = ed.getContent().replace(/(<([^>]+)>)/ig,"").length;

//setting up the text string that will display in the path area
htmlcount = "HTML Character Count: " + tinylen + "/" + tinymax;

//if the user has exceeded the max turn the path bar red.
if (tinylen > tinymax){

// place text string in path bar
if ( $('#max_char_string').size() ){
$('#max_char_string').html( '&nbsp;' + htmlcount);
}
else {
$("div#"+ed.id+"_path_row").append('<span id="max_char_string">&nbsp;'+htmlcount+'</span>')
}

// prevent insertion of typed character
e.preventDefault();
e.stopPropagation();
return false;
}

关于javascript - TinyMCE 文本编辑器最大字符限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11790474/

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