问题 TinyMCE文本编辑器最大字符限制


我正在使用TinyMCE <textarea>。我的要求是将字符大小限制为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个字符。
欢迎任何建议。


8587
2017-08-03 06:33


起源

可能会有所帮助: stackoverflow.com/questions/5533053/textarea-character-limit 我以前没有使用过tinymce但是redactor使用了textarea所以我认为tinymce也使用了textarea。 - Wern Ancheta
感谢您的回复,但textarea的正常限制不适用于tincymce。 - Prathamesh mhatre
可能重复 限制tinyMCE中的字符数 - naXa


答案:


我建议您在KeyDp上执行代码,因为在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;
      } 

9
2017-08-03 07:41





我有同样的问题,发现这个链接非常有用: http://www.ryann.ca/?p=186

虽然我稍微改变了这一点,以便它从textarea直接读取maxlength的属性。

tinyMCE.init({
    // Options
    setup : function(ed) {
        ed.onKeyUp.add(function(ed, e) {
            var tinylen, htmlcount, maxlength = $("#" + tinyMCE.activeEditor.id).attr("maxlength");
            if (maxlength) {
                // grabbing the length of the curent editors content
                tinylen = ed.getContent().length;

                htmlcount = "HTML Character Count: " + tinylen + "/" + maxlength;
                if (tinylen > maxlength) {
                    htmlcount = "<span style='font-weight:bold; color: #f00;'>" + htmlcount + "</span>";
                }

                // write the html count into the path row of the active editor
                tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id+ '_path_row'), htmlcount);
            }
        });//ed.onKeyUp.add
    }//setup
});

2
2017-08-20 14:09



当我运行你的解决方案时,我得到“ed.onKeyUp is undefined”错误。我该如何解决?谢谢!我使用的是4.0.4版。 - curious1
你需要确保它在里面 setup : function(ed) 看到更新的答案。 - John Magnolia
约翰,非常感谢回复我。我按照你提到的链接(ryann.ca/?p=186)和函数(ed)确实是setup的属性值,这是一个选项。你在使用tinyMCE 4.x吗?谢谢! - curious1
嗨约翰,我发现了这个: stackoverflow.com/questions/16939148/...。似乎事件API在tinyMCE 4中发生了变化。您是否介意发布另一个符合tinyMCE 4的回复?谢谢! - curious1


希望它会工作:)

setup: function(ed) {            
            var maxlength = parseInt($('#'+(ed.id)).attr("maxlength"));
            var count = 0;
            ed.on('keydown', function(e) {
                count++;
                if (count >= maxlength)
                {
                    alert("false");
                    return false;
                }
            });
        },

1
2017-10-09 09:44