5

自从我使用 CKEditor ( http://ckeditor.com/ ) 以来,我遇到了一个问题。问题是我找不到使编辑器只读的方法,而且我不能只使用文本区域,因为我想保持一致性。我已经在 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 完整包)

提前致谢!:)

4

7 回答 7

9

在文档中readOnly您可以将配置设置为 readOnly

config.readOnly = true;

还有一个示例显示通过方法设置它

editor.setReadOnly( true);
于 2013-04-26T13:04:09.947 回答
5

try with following lines,

<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>
于 2014-07-09T16:01:30.187 回答
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">&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>
于 2013-04-26T13:05:08.150 回答
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;

于 2014-03-13T12:36:20.417 回答
0

使用 4.5.4 版,您可以这样做:

$('#idElement).ckeditorGet().setReadOnly(true);
于 2018-02-02T12:10:14.457 回答
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>';
?>
于 2016-05-12T07:15:51.413 回答
0

在第 5 版中,我这样做:

ClassicEditor
    .create( document.querySelector( '.editor' ), {
        removePlugins: ['Title'],
        licenseKey: '',

    } )
    .then( editor => {
        window.editor = editor;
        editor.isReadOnly = true;


    } )
    .catch( error => {
        console.error( 'Oops, something went wrong!' );
        console.error( 'Please, report the following error on https://github.com/ckeditor/ckeditor5/issues with the build id and the error stack trace:' );
        console.warn( 'Build id: efxy8wt6qchd-qhxgzg9ulnyo' );
        console.error( error );
    } );
于 2021-12-07T10:58:20.183 回答