我使用CKEditor(http://ckeditor.com/)后遇到了问题。问题是我找不到编辑ReadOnly的方法,因为我想保持一致性,所以我不能只使用textarea。我已经在StackOwerflow上看到过很多这样的问题,但是它们都没有工作或者太旧了。
到目前为止我的代码只是显示/初始化编辑器:
$(document).ready(function(){
CKEDITOR.replace( 'ckeditor', {
on: {
// Check for availability of corresponding plugins.
pluginsLoaded: function( evt ) {
var doc = CKEDITOR.document, ed = evt.editor;
if ( !ed.getCommand( 'bold' ) )
doc.getById( 'exec-bold' ).hide();
if ( !ed.getCommand( 'link' ) )
doc.getById( 'exec-link' ).hide();
}
}
});
});
我使用最新的CKEditor版本(v.4.1.1完整包)
提前致谢! :)
答案 0 :(得分:8)
答案 1 :(得分:3)
尝试以下行,
<textarea id="editor1" name="editor1" ></textarea>
<textarea id="editor2" name="editor2" ></textarea>
<input type="button" onclick="EnableEditor2()" value="EnableEditor2" />
<script>
$(document).ready(function () {
//set editor1 readonly
CKEDITOR.replace('editor1', {readOnly:true});
CKEDITOR.replace('editor2');
//set editor2 readonly
CKEDITOR.instances.editor2.config.readOnly = true;
});
function EnableEditor2() {
CKEDITOR.instances.editor2.setReadOnly(false);
}
</script>
答案 2 :(得分:1)
他们说,这应该有用
var editor;
// The instanceReady event is fired, when an instance of CKEditor has finished
// its initialization.
CKEDITOR.on( 'instanceReady', function( ev )
{
editor = ev.editor;
// Show this "on" button.
document.getElementById( 'readOnlyOn' ).style.display = '';
// Event fired when the readOnly property changes.
editor.on( 'readOnly', function()
{
document.getElementById( 'readOnlyOn' ).style.display = this.readOnly ? 'none' : '';
document.getElementById( 'readOnlyOff' ).style.display = this.readOnly ? '' : 'none';
});
});
function toggleReadOnly( isReadOnly )
{
// Change the read-only state of the editor.
// http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setReadOnly
editor.setReadOnly( isReadOnly );
}
和html
<form action="sample_posteddata.php" method="post">
<p>
<textarea class="ckeditor" id="editor1" name="editor1" cols="100" rows="10"><p>This is some <strong>sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p></textarea>
</p>
<p>
<input id="readOnlyOn" onclick="toggleReadOnly();" type="button" value="Make it read-only" style="display:none" />
<input id="readOnlyOff" onclick="toggleReadOnly( false );" type="button" value="Make it editable again" style="display:none" />
</p>
</form>
答案 3 :(得分:0)
来源:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.readOnly http://rev.ckeditor.com/ckeditor/trunk/7657/_samples/readonly.html
CKEDITOR.config.readOnly = true;
答案 4 :(得分:0)
检查一下。这个想法是,如果用户登录的系统的分类不是“BPPA”,则CK编辑器应被禁用且只读。如果用户的分类是BPPA,则CK编辑器是可编辑的。请注意,这些代码部分实际上是在PHP中。他们需要一个可运作的数据库才能工作,但我想你可能会得到这个想法,并且能够发挥自己的魔力。
<?php
//This line is to disable PART A if classification != 'BPPA'
$bppa = mysql_query("SELECT * from roles WHERE username = '$_SESSION[username]'");
$bppa_row = mysql_fetch_array($bppa);
if($bppa_row['classification'] != 'BPPA'){
$disabled = 'disabled = "disabled"';
}else{
$disabled = "";
}
//ends here
?>
然后,将$ disable应用于您的文本区域:
<?php
echo '<textarea class="ckeditor" '.$disabled.' name="content' . $n . '" id="content' . $n . '">' . $saved . '</textarea>';
?>
答案 5 :(得分:0)
版本4.5.4你可以用:
$('#idElement).ckeditorGet().setReadOnly(true);