解决UEditor超出最大字数后只提示不限制的问题

本文介绍如何在UEditor中实现精确的字数限制功能,防止用户输入超过设定的最大字数,通过修改源码实现即时阻止多余输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


最近项目用到百度额UEditor文本编辑器,今天测试向我提出了一个问题。就是在输入的文字超过默认的最大字数限制之后,虽然提示“字数超过最大范围,服务器可能拒绝保存”,但是仍然可以点击保存按钮进行保存。


现在想要实现在达到最大字数的时候,就禁止再继续输入了。就像之前微博那样最大140字超过就不能输入那样。


查了官方文档,无果。然后百度到的答案几乎一致。如下图所示:

第一步:在ueditor.all.js中找到这两行注释掉
countDom.innerHTML = errMsg;
editor.fireEvent(“wordcountoverflow”)
第二步:在注释点的两行下面写上这三行就搞定了
var content = editor.getContentTxt();
editor.setContent(content.substring(0,maxwordsnum));
editor.focus(true);

然而,参照此方法并没有用,F12调试js代码发现,maxwordsnum
没有定义啊。


于是试着修改了一下这段代码


function setCount(editor,ui) {
                editor.setOpt({
                    wordCount:true,
                    maximumWords:10000,
                    wordCountMsg:editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
                    wordOverFlowMsg:editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
                });
                var opt = editor.options,
                    max = opt.maximumWords,
                    msg = opt.wordCountMsg ,
                    errMsg = opt.wordOverFlowMsg,
                    countDom = ui.getDom('wordcount');
                if (!opt.wordCount) {
                    return;
                }
                var count = editor.getContentLength(true);
                if (count > max) {
//                    countDom.innerHTML = errMsg;
//                    editor.fireEvent("wordcountoverflow");
                    debugger;
                    var content = editor.getContentTxt();
                    editor.setContent(content.substring(0,max));
                    editor.focus(true);
                } else {
                    countDom.innerHTML = msg.replace("{#leave}", max - count).replace("{#count}", count);
                }
            }

其实只是将maxwordsnum修改成了max变量,成功。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值