disable TinyMCE text editor via Jquery

时光毁灭记忆、已成空白 提交于 2019-12-24 17:14:09

问题


I ahave multiple Tiny MCE editors which I am trying to disable editing on. I have tried multiple variations of:

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);
        }

with my editors all defined similarly like:

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');
              }

            });
          }

        });

currently with what I am trying, I am getting a console error: Cannot read property 'getBody' of null


回答1:


You have a typo in your disable code:

tinymce.get('#rte').getBody().setAttribute('contenteditable', false);

This line must be:

tinyMCE.get('rte').getBody().setAttribute('contenteditable', false);

So remove the # symbol.

Because you already used setMode you can do:

tinyMCE.get('rte').setMode('readonly');

or

tinyMCE.get('rte').setMode('code');

Snippet (fiddle: 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');
        }

      });
    }
  });
});
<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>



回答2:


Your call to tinymce.get() is incorrect.

The API for get (https://www.tinymce.com/docs/api/tinymce/root_tinymce/#get) states:

Returns a editor instance by id.: get(id:String):tinymce.Editor

Parameters: id (String) - Editor instance id or index to return.

This is not a jQuery API call so your call:

tinymce.get('#rteSmall')

...should likely be...

tinymce.get('rteSmall')


来源:https://stackoverflow.com/questions/38877891/disable-tinymce-text-editor-via-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!