简体   繁体   English

ckeditor:如何在焦点内选择编辑器中的所有文本?

[英]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. 我正在使用ckeditor进行文本格式设置,并且编辑器中有默认文本,我希望随时显示选中的文本。

Here is my code 这是我的代码

HTML: HTML:

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

Javascript: 使用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: 如果您使用了JQuery,它将看起来像:

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

JSFiddle - http://jsfiddle.net/s6Z82/5/ 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. 现在唯一需要弄清楚的是如何从CKEDITOR.instances.FirstTextEditor.on回调中获取当前元素,然后完成。

Why the setTimeout(.., 0)? 为什么要使用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. 原因似乎在Chrome中e.preventDefault()不会阻止它放弃选择并将光标放在行尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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