summernote - 如何限制summernote中文本区域的字数

标签 summernote

我正在尝试限制夏日笔记中允许的字数。

例如,我只想限制最多 50 个字。

你有什么想法吗?

$(document).ready(function() {
    $('#summernote').summernote({
        height: 20,
   });
});

最佳答案

我想为这篇文章做出贡献,因为这帮助我找到了我想要的解决方案。看来建议的答案尚未被接受。

我为 maxLength 创建了一个变量,以下是我配置 Summernote 回调的方式。请注意,我使用的 Summernote 版本的编辑区域为 .note-editable。下面的解决方案是 typescript ,我有一个目标范围,它显示剩余的字符数。

callbacks: {
onKeydown(e) {

  // The text method will ignore HTML tags and preserve non-breaking
  // space allowing for a true character count

  let content: string = $('.note-editable').text();

  // This checks if the character is alphanumeric (i.e. not a backspace or return key)

  let isCharacter: boolean = (/[a-zA-Z0-9-_ ]/.test(String.fromCharCode(e.keyCode)));

  // If the current content length is at max, preventDefault will disallow 
  // any more characters

  if (isCharacter) {
    if (content.length >= maxLength) {
      e.preventDefault();
    }
  }
},
onKeyup(e) {

  // After the result of checking the character and max length exceeded, 
  // take the resulting count and determine and display characters remaining

  let content: string = $('.note-editable').text();
  let charactersRemaining: number = maxLength - content.length;
  $('#SomeDivId span').text(charactersRemaining);
}

关于summernote - 如何限制summernote中文本区域的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796758/

相关文章:

Summernote 编辑器未在页面上初始化

javascript - Summernote图片通过AJAX上传导致页面刷新

javascript - Summernote - 聚焦,在编辑器末尾插入光标

wysiwyg - Summernote:如何添加 "italic"按钮,使其显示在粗体和下划线按钮旁边?

javascript - 覆盖 javascript 文件 (summernote.js) 中的一些函数

summernote - 如何自定义 Summernote 文本编辑器的宽度?

jquery - 关键是无效的 JQuery 语法,因为 Summernote Html 编辑器缺少右括号

javascript - 无法在 spring 元素中显示 Summernote textarea

Flutter 中的 Html 所见即所得编辑器?

jquery - Summernote js编辑器Serialize()获取HTML代码?