您的禁用代码中有一个拼写错误:
tinymce.get('#rte').getBody().setAttribute('contenteditable', false);
该行必须是:
tinyMCE.get('rte').getBody().setAttribute('contenteditable', false);
所以去掉#符号。
因为你已经使用了 setMode 你可以这样做:
tinyMCE.get('rte').setMode('readonly');
or
tinyMCE.get('rte').setMode('code');
片段(小提琴:HERE https://jsfiddle.net/8kwnut6o/1/):
$(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>