繁体   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