最简单的方法:
禁用CKEditor:
CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(true);
启用CKEditor:
CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(false);
我在使用CodeIgniter。 在视图文件中,我像这样使用它:
<script type="text/javascript">
$(document).ready(function() {
CKEDITOR.config.readOnly = true;
});
</script>
.ckeditorGet()
jQuery函数快速访问CKEditor的API。setReadOnly
编辑器函数:$( _your_textarea_ ).ckeditorGet().setReadOnly();
.setReadOnly( false );
readOnly
配置值,当你启动编辑器时立即将其设置为只读模式:$( _your_textarea_ ).ckeditor({
readOnly: true
});
假设您已经包含了jQuery适配器,以下代码将使其只读。如果还没有包括jQuery适配器,您可以从示例中获取。
<div class="wrapper">
<form id="myfrm" name="myfrm" class="myfrm" action="" method="post">
<textarea id="myeditor" name="myeditor"></textarea>
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
</div>
和js
$(document).ready(function(e) {
var myeditor = $('#myeditor');
myeditor.ckeditor();
myeditor.ckeditorGet().config.resize_enabled = false;
myeditor.ckeditorGet().config.height = 200;
myeditor.ckeditorGet().config.readOnly = true;
});
$(document).ready(function(){
//put ckeditor initialization (the above) here.
$('#myselect').change(function(){
var x = $(this);
if(x.val()=='enable'){
myeditor.removeAttr('disabled');
}else if(x.val()=='disable'){
myeditor.attr('disabled','disabled');
}
myeditor.ckeditorGet().destroy();
myeditor.ckeditor();
});
});
disabled="disabled"
,并在销毁当前实例后重新加载ckeditor。请查看JSFiddle示例2。
来自:CKSource 论坛
// Temporary workaround for providing editor 'read-only' toggling functionality.
( function()
{
var cancelEvent = function( evt )
{
evt.cancel();
};
CKEDITOR.editor.prototype.readOnly = function( isReadOnly )
{
// Turn off contentEditable.
this.document.$.body.disabled = isReadOnly;
CKEDITOR.env.ie ? this.document.$.body.contentEditable = !isReadOnly
: this.document.$.designMode = isReadOnly ? "off" : "on";
// Prevent key handling.
this[ isReadOnly ? 'on' : 'removeListener' ]( 'key', cancelEvent, null, null, 0 );
this[ isReadOnly ? 'on' : 'removeListener' ]( 'selectionChange', cancelEvent, null, null, 0 );
// Disable all commands in wysiwyg mode.
var command,
commands = this._.commands,
mode = this.mode;
for ( var name in commands )
{
command = commands[ name ];
isReadOnly ? command.disable() : command[ command.modes[ mode ] ? 'enable' : 'disable' ]();
this[ isReadOnly ? 'on' : 'removeListener' ]( 'state', cancelEvent, null, null, 0 );
}
}
} )();
使用方法:
// Turn CKEditor into 'ready-only' mode or vice versa.
CKEDITOR.instances.editor1.readOnly( true );
CKEDITOR.instances.editor1.readOnly( false );
我将
我认为您没有仔细阅读文档......它表明Ckeditor具有某些实例名称,并且在页面的标签中使用该实例名称,设置InstanceNameOfCkEditor.config.readOnly = true;
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setReadOnly
CKEDITOR.instances.instance_name.on('instanceReady', function(ev) {
var editor = CKEDITOR.instances.instance_name;
//for disable
editor.setReadOnly();
//for enable
editor.setReadOnly(false);
});
你可以尝试这个,这是最好的解决方案。
var existingEditor = CKEDITOR.instances && CKEDITOR.instances[element.id];
try {
if (existingEditor && typeof(existingEditor) !== "undefined" && typeof(existingEditor.setReadOnly) !== "undefined") {
if (el.prop("disabled")) {
existingEditor.setReadOnly(true);
} else {
existingEditor.setReadOnly(false);
}
}
} catch (ex) {
//do nothing
}