2013-04-26 21 views
4

我使用CKEditor时出现问题(http://ckeditor.com/)。问题是,我找不到一种方法来编辑ReadOnly,并且我不能只使用textarea,因为我想保持一致性。我已经在StackOwerflow上看到过很多像这样的问题,但没有一个能工作或者太旧。CKEditor只读

到目前为止我的代码只是为了显示/初始化编辑:

$(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

你有没有试图使之成为一个iframe中,而不是textarea的?因为这会从另一页面加载内容,并以这种方式使其成为ReadOnly。 – 2013-04-26 13:03:45

回答

7

readOnly您可以设置配置为只读

config.readOnly = true; 

还有一个example这显示了通过一种方法设置它的文档

editor.setReadOnly(true); 
1

你试过 this

他们说,这应该工作

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">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</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> 
+0

对不起(我是CKEditor的新手),但是我应该怎么用editor.setReadOnly替换编辑器? – 2013-04-26 13:35:16

+0

@picios:最好链接到[nightly samples](http://nightly.ckeditor.com/standard/samples/readonly.html)。我们比一年前停止使用Trac。 – Reinmar 2013-04-26 17:09:19

2

尝试用以下命令行,

<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> 
+0

你可以添加一个解释为什么这个工程? – secretformula 2014-07-09 16:34:56

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 
?> 

然后,应用$禁用对文本区域:

<?php 
echo '<textarea class="ckeditor" '.$disabled.' name="content' . $n . '" id="content' . $n . '">' . $saved . '</textarea>'; 
?> 
0

与版本4.5.4你可以做到这一点:

$('#idElement).ckeditorGet().setReadOnly(true);