2016-08-10 33 views
1

我有多个Tiny MCE编辑器,我试图禁用编辑。我已经尝试的多种变化:定义同样喜欢通过jquery禁用TinyMCE文本编辑器

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

与我的编辑都:

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

      }); 
      } 

     }); 
目前有什么,我想,我得到一个控制台错误

Cannot read property 'getBody' of null

+0

检查控制台。如果有任何错误。 。 –

+0

如果有多个,请尝试此操作。 。 tinyMCE.get('rte')。getBody()。setAttribute('contenteditable',false); –

+0

也从版本4.3.x开始,您可以使用以下代码进行只读模式.. 其类似tinymce.activeEditor.setMode('readonly'); –

回答

6

你在你选择禁用代码有一个错字:

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

此行必须是:

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

所以删除#符号。

因为你已经使用setMode你可以这样做:

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

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

片段(小提琴: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

您的来电to tinymce.get()不正确。

对于GET(https://www.tinymce.com/docs/api/tinymce/root_tinymce/#get)的API规定:

按id返回编辑器实例:得到(ID:字符串):tinymce.Editor

参数: ID(字符串) - 编者实例ID或索引返回。

这不是一个jQuery的API调用,以便您的来电:

tinymce.get('#rteSmall') 

...应该有可能是......

tinymce.get('rteSmall')