如何使用jQuery清除CKEditor?

23

如何在点击按钮/链接时使用 jQuery 清除 ckeditor 文本区域?

我尝试过这样做:$("textarea.editor").val('');$("textarea.editor1").val('');,我在末尾加上了 1,因为在 PHP 中初始化编辑器时有这一行:$ckeditor->editor('editor1', $nDetails);

非常感谢任何帮助。

<p>Details: 

    <?php

    // Helper function for this sample file.
    function printNotFound( $ver )
    {
        static $warned;

        if (!empty($warned))
            return;

        echo '<p><br><strong><span class="error">Error</span>: '.$ver.' not found</strong>. ' .
            'This sample assumes that '.$ver.' (not included with CKFinder) is installed in ' .
            'the "ckeditor" sibling folder of the CKFinder installation folder. If you have it installed in ' .
            'a different place, just edit this file, changing the wrong paths in the include ' .
            '(line 57) and the "basePath" values (line 70).</p>' ;
        $warned = true;
    }



    include_once '../ckeditor/ckeditor.php' ;
    require_once '../ckfinder/ckfinder.php' ;

    // This is a check for the CKEditor class. If not defined, the paths in lines 57 and 70 must be checked.
    if (!class_exists('CKEditor'))
    {
        printNotFound('CKEditor');
    }
    else
    {
        $initialValue = $pageContent;

        $ckeditor = new CKEditor( ) ;
        $ckeditor->basePath = '../ckeditor/' ;

        // Just call CKFinder::SetupCKEditor before calling editor(), replace() or replaceAll()
        // in CKEditor. The second parameter (optional), is the path for the
        // CKFinder installation (default = "/ckfinder/").
        CKFinder::SetupCKEditor( $ckeditor, '/ckfinder/' ) ;

        $ckeditor->editor('editor1', $nDetails);
    }

    ?></p>

编辑器是否有一个<div>或某个容器来容纳文本?能否展示编辑器控件的HTML代码? - MattW
我已经放在一个p标签中,并且在一个表单中有几个表单字段,而这个表单位于一个div中。 - user875293
我曾尝试使用演示(http://ckeditor.com/demo)和Firebug编写一些jQuery代码,但很遗憾...没有成功。 - MattW
6个回答

59

CKEDITOR.instances.editor1.setData('');

其中editor1是您的CKEDITOR字段的ID。


对我来说,在IE中它可以工作(javascript控制台中也没有错误)。版本:9.0.8112.16421 64位版,更新版本:9.0.10。 - Shawn
这在Chrome浏览器中的CKEditor 4.2.3标准版本对我有效。谢谢。 - Sean Glover
CKEDITOR.instances.editor1.setData(''); 这个代码可以清空编辑器的内容,但是当调用 CKEDITOR.instances.editor1.insertHtml('some content'); 时,它不再向已清空的编辑框中插入任何内容了... - Mike D3ViD Tyson
1
【已解决】将 CKEDITOR.instances.editor1.setData('some html content'); 改为 CKEDITOR.instances.editor1.setData(''); - Mike D3ViD Tyson
你能写出完整的解决方案吗?你如何获取CKEditor字段的ID? - Jun Dalisay

18

以下代码清除CKEditor文本区域中的值。这也适用于4.4版本。

      CKEDITOR.instances['content'].setData('');

Content是具体文本区域的ID。


2
尝试一下,可能会有帮助。
function CKupdate(){
    for ( instance in CKEDITOR.instances ){
        CKEDITOR.instances[instance].updateElement();
    }
    CKEDITOR.instances[instance].setData('');
}   

$.ajax({
   url: $('#CommunicationForm').attr("action"),
   type: $('#CommunicationForm').attr("method"),
   data: $('#CommunicationForm').serialize(),
   success: function (e) {
       if (e.error) {
          alert(e.error);
       } else {
          //Doing Clear here                          
          CKupdate();
       }
   },
   error: function (jqXHR, Status, text) {
        alert("error! " + text);
   }   
});

当您想要清除CKEditor文本时,请调用CKupdate()函数。


非常好,谢谢。我已经在函数顶部使用了updateElement(),所以我只是在.done()回调中添加了setData('')部分 :) - Can Rau
对我有用,谢谢 - undefined

1

这是清除CK Editor数据的最佳方法

CKEDITOR.instances['yourCKeditorID'].setData('');

0
在我的代码中,我必须以编程方式更改ckeditor中的文本,但直到我进行了延迟设置,它才起作用。这可能会有所帮助。
_.defer(function () {
    it._controls.wysiwyg.setData(bodyText); // by the direct reference
    //CKEDITOR.instances.editor1.setData(''); // or this way like in the example
}); 

0

你可以使用这个:

        $(".ck-blurred p").html("") //empty ckeditor

尽管此代码可能回答了问题,但最好包含一些“上下文”来解释其工作方式以及何时使用。仅有代码的答案从长远来看并不有用。 - PCM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接