通过Jquery禁用TinyMCE文本编辑器

Gre*_*egH 1 .net javascript jquery tinymce

我有多个Tiny MCE编辑器,我试图禁用编辑.我尝试了多种变体:

if(@Model.hasRegular.ToString().ToLower()) 
        {
          tinymce.get('#rte').getBody().setAttribute('contenteditable', false);
        }
        if(@Model.hasSmall.ToString().ToLower())
        {
          tinymce.get('#rteSmall').getBody().setAttribute('contenteditable', false);
        }
        if(@Model.isOneSize.ToString().ToLower())
        {
          tinymce.get('#rteOneSize').getBody().setAttribute('contenteditable', false);
        }
Run Code Online (Sandbox Code Playgroud)

与我的编辑所有定义类似:

tinymce.init({
          selector: '#rte',
          toolbar: 'bold italic underline',
          height: '200px',
          width: '420px',
          elementpath: false,
          menubar: false,
          content_style: "div {border: 50px solid red;}",

          setup: function (ed) {
            ed.on('init', function () {
              this.getDoc().body.style.fontSize = '16px';
              this.getDoc().body.style.fontFamily = 'Helvetica';
              //disables editing for non admins
              if ($('#nonAdmin').length) {
                this.setMode('readonly');
              }

            });
          }

        });
Run Code Online (Sandbox Code Playgroud)

目前我正在尝试,我收到一个控制台错误: Cannot read property 'getBody' of null

gae*_*noM 6

您的禁用代码中有拼写错误:

tinymce.get('#rte').getBody().setAttribute('contenteditable', false);
Run Code Online (Sandbox Code Playgroud)

这一行必须是:

tinyMCE.get('rte').getBody().setAttribute('contenteditable', false);
Run Code Online (Sandbox Code Playgroud)

所以删除#符号.

因为您已经使用了setMode,所以您可以:

tinyMCE.get('rte').setMode('readonly');
Run Code Online (Sandbox Code Playgroud)

要么

tinyMCE.get('rte').setMode('code');
Run Code Online (Sandbox Code Playgroud)

片段(小提琴:HERE):

$(function () {
  $('#nonAdmin').on('change', function(e) {
    tinyMCE.get('rte').getBody().setAttribute('contenteditable', !this.checked);
  });


  tinymce.init({
    selector: '#rte',
    toolbar: 'bold italic underline',
    height: '200px',
    width: '420px',
    elementpath: false,
    menubar: false,
    content_style: "div {border: 50px solid red;}",

    setup: function (ed) {
      ed.on('init', function () {
        this.getDoc().body.style.fontSize = '16px';
        this.getDoc().body.style.fontFamily = 'Helvetica';
        //disables editing for non admins
        if ($('#nonAdmin').prop('checked')) {
          this.setMode('readonly');
        }

      });
    }
  });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.4.1/tinymce.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.4.1/jquery.tinymce.min.js" type="text/javascript"></script>


<h1>TinyMCE Quick Start Guide</h1>
<form method="post">
    Disable: <input type="checkbox" id="nonAdmin">
    <textarea id="rte">Hello, World!</textarea>
</form>
Run Code Online (Sandbox Code Playgroud)