使用tinymce 4.0文本编辑器如何限制字符?

时间:2014-09-27 16:17:14

标签: javascript tinymce

这是我的讽刺剧本:

<script>
      tinymce.init({
          menubar: false,
          selector: "textarea",
          plugins: [
              "advlist autolink lists link image charmap print preview anchor",
              "searchreplace visualblocks code fullscreen",
              "insertdatetime media table contextmenu paste"
          ],
          toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
      });
    </script>

如何将字符限制为500?

2 个答案:

答案 0 :(得分:0)

它可以工作,但我不知道怎么让它可以按删除键......

  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 :(得分:0)

我添加了“stopPropagation”行,以防止用户继续输入字符: 1 - 在textarea的html代码中,必须包含maxlength和Id的值 2 - 在脚本部分,下面的代码。如果需要,请取消注释alert()行,然后发送消息。

<script type="text/javascript">
  tinymce.init ({
    ...
    ...
      setup: function(ed) {
        var maxlength = parseInt($("#" + (ed.id)).attr("maxlength"));
        var count = 0;
        ed.on("keydown", function(e) {
          count++;
          if (count > maxlength) {
            // alert("You have reached the character limit");
            e.stopPropagation();
            return false;
          }
        });
     },
<textarea type="text" id="test" name="test" maxlength="10"></textarea>