簡體   English   中英

ckeditor:如何在焦點內選擇編輯器中的所有文本?

[英]ckeditor: How to select all text inside editor on focus?

我正在使用ckeditor進行文本格式設置,並且編輯器中有默認文本,我希望隨時顯示選中的文本。

這是我的代碼

HTML:

<textarea id="FirstTextEditor" >Hello</textarea>

使用Javascript:

$(document).ready(function(){
    CKEDITOR.instances.FirstTextEditor.on('focus', function () {
        // What is code i want write to select all text on focus?
    });
});

您可以像這樣簡單地實現您想要的:

CKEDITOR.instances.["your instance here"].on( 'focus', function () { this.execCommand( 'selectAll' ); });

如果您使用了JQuery,它將看起來像:

$(document).ready(function(){
    $('textarea').focus(function(e){
        var that = this;
        setTimeout(
            function() {
                that.select()
            }, 0);
    });
});

JSFiddle- http://jsfiddle.net/s6Z82/5/

現在唯一需要弄清楚的是如何從CKEDITOR.instances.FirstTextEditor.on回調中獲取當前元素,然后完成。

為什么要使用setTimeout(..,0)?

原因似乎在Chrome中e.preventDefault()不會阻止它放棄選擇並將光標放在行尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM