简体   繁体   中英

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

I am using ckeditor for text formating and there is default text inside editor which I want to show selected on focus at all time.

Here is my code

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' ); });

If you used JQuery, it would look like:

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

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

The only thing you now have to figure out is how do you get the current element from the CKEDITOR.instances.FirstTextEditor.on callback and you are done.

Why the setTimeout(.., 0)?

Cause seems in Chrome e.preventDefault() does not stop it from discarding the select and putting the cursor to the end of the line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM