将CKEditor字数仅应用于特定字段

时间:2019-01-11 16:40:08

标签: javascript jquery html ckeditor4.x

如何仅将“ maxWordCount:50”设置应用于具有“ simple”类的textarea字段?这可能吗?如果不是,是否可以将其应用于具有名称字段的所有textarea字段[141]?当前,所有字段都会自动应用maxWordCount设置。谢谢!

CKEDITOR.config.extraPlugins = 'media,autolink,wordcount,notification';
CKEDITOR.config.allowedContent = true;
CKEDITOR.config.skin = 'moonocolor';
CKEDITOR.config.scayt_autoStartup = true;
CKEDITOR.config.wordcount = {
  maxWordCount: 50
};

function ckeditor(elems) {
  $(elems).each(function(i, elem) {
    var height = '200px';
    if ($(elem).hasClass('short')) {
      height = '75px';
    }

    if ($(elem).hasClass('simple')) {
      CKEDITOR.replace(elem, {
        toolbar: [
          { name: 'clipboard', items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', 'Undo', 'Redo' ] },
          { name: 'editing', items: [ 'Scayt' ] },
          { name: 'links', items: [ 'Link', 'Unlink', 'Autolink', 'Media', 'Source'] },
          { name: 'insert', items: [ 'Table', 'SpecialChar', 'Templates', 'Maximize'] },
          { name: 'basicstyles', items: [ 'Bold', 'Italic', 'Strike', 'Underline', 'RemoveFormat' ] },
          { name: 'paragraph', items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent' ] }
        ],
        height: height
      });

    }
    else if($(elem).hasClass('nofontstyling')) {

      CKEDITOR.replace(elem, {
        toolbar: [
          { name: 'clipboard', items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] },
          { name: 'editing', items: [ 'Scayt' ] },
          { name: 'links', items: [ 'Link', 'Unlink', 'Autolink' ] },
          { name: 'insert', items: [ 'Media', 'Table', 'Anchor', 'SpecialChar', 'Templates', 'Maximize', 'Source' ] },
          '/',
          { name: 'basicstyles', items: [ 'Bold', 'Italic', 'Strike', 'Underline', '-', 'CopyFormatting', 'RemoveFormat' ] },
          { name: 'paragraph', items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ] }
        ],
        height: height
      });

    }
    else {
      CKEDITOR.replace(elem, {
        height: height
      });
    }
  });
}



$(document).ready(function() {
  ckeditor($('textarea.editor'));
});
<textarea name="fields[141]" id="fields[141]" class="simple editor">&lt;p&gt; content goes here  &lt;/p&gt;</textarea>
<textarea name="fields[30]" id="fields[30]" class=" editor"></textarea>

1 个答案:

答案 0 :(得分:0)

这应该有效...

 CKEDITOR.config.extraPlugins = 'media,autolink,wordcount,notification';
CKEDITOR.config.allowedContent = true;
CKEDITOR.config.skin = 'moonocolor';
CKEDITOR.config.scayt_autoStartup = true;

function ckeditor(elems) {
  $(elems).each(function(i, elem) {
    var height = '200px';
    if ($(elem).hasClass('short')) {
      height = '75px';
    }

    if ($(elem).hasClass('simple')) {
      CKEDITOR.replace(elem, {
        toolbar: [
          { name: 'clipboard', items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', 'Undo', 'Redo' ] },
          { name: 'editing', items: [ 'Scayt' ] },
          { name: 'links', items: [ 'Link', 'Unlink', 'Autolink', 'Media', 'Source'] },
          { name: 'insert', items: [ 'Table', 'SpecialChar', 'Templates', 'Maximize'] },
          { name: 'basicstyles', items: [ 'Bold', 'Italic', 'Strike', 'Underline', 'RemoveFormat' ] },
          { name: 'paragraph', items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent' ] }
        ],
        height: height,
         wordcount:
                    { maxWordCount: 50}
      });

    }
    else if($(elem).hasClass('nofontstyling')) {

      CKEDITOR.replace(elem, {
        toolbar: [
          { name: 'clipboard', items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] },
          { name: 'editing', items: [ 'Scayt' ] },
          { name: 'links', items: [ 'Link', 'Unlink', 'Autolink' ] },
          { name: 'insert', items: [ 'Media', 'Table', 'Anchor', 'SpecialChar', 'Templates', 'Maximize', 'Source' ] },
          '/',
          { name: 'basicstyles', items: [ 'Bold', 'Italic', 'Strike', 'Underline', '-', 'CopyFormatting', 'RemoveFormat' ] },
          { name: 'paragraph', items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ] }
        ],
        height: height
      });

    }
    else {
      CKEDITOR.replace(elem, {
        height: height
      });
    }
  });
}



$(document).ready(function() {
  ckeditor($('textarea.editor'));
});