2013-04-26 66 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);