javascript 如何在ace编辑器上设置焦点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7050931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:48:16  来源:igfitidea点击:

How to set focus on the ace editor?

javascriptjqueryjquery-uiace-editor

提问by Ameet

I am using the ace editor component from ajax.org inside a jquery tab interface. Each tab will contain a separate ace editor. Whenever I switch to a new tab, the editor in it won't get the focus.

我在 jquery 选项卡界面中使用来自 ajax.org 的 ace 编辑器组件。每个选项卡将包含一个单独的 ace 编辑器。每当我切换到新选项卡时,其中的编辑器都不会获得焦点。

I can detect when a tab is selected by binding to the "tabsshow" event of jquery UI. Does any one know how to set focus to the editor in it if say the id of my editor div is editor-tab-0 for the first tab, and so on...?

我可以通过绑定到 jquery UI 的“tabsshow”事件来检测何时选择了选项卡。如果说我的编辑器 div 的 id 是第一个选项卡的 editor-tab-0,依此类推,有谁知道如何将焦点设置到其中的编辑器?

Please can some one help?

请有人帮忙吗?

回答by rockvilla

editor.focus(); //To focus the ace editor
var n = editor.getSession().getValue().split("\n").length; // To count total no. of lines
editor.gotoLine(n); //Go to end of document

回答by Julian

To focus to the end:

聚焦到最后:

editor.focus();
editor.navigateFileEnd();

Thanks to @a-user

感谢@a-user

回答by daniellmb

Nice solution, but I wanted to go to the end of the last line not the beginning of the last line.

不错的解决方案,但我想转到最后一行的末尾而不是最后一行的开头。

//To focus the ace editor
editor.focus();
session = editor.getSession();
//Get the number of lines
count = session.getLength();
//Go to end of the last line
editor.gotoLine(count, session.getLine(count-1).length);

回答by Paul Beusterien

Here's an excerpt from my code that sets the focus on an Ace edit session in a jQuery UI tab:

这是我的代码的摘录,它在 jQuery UI 选项卡中将焦点设置在 Ace 编辑会话上:

    $('#tabs_div').tabs(
        {
            select : function(event, ui) {
                        var tabName = ui.panel.id;
                        var doc = docs.get(tabName);  // look up the EditSession
                        var session = env.split.setSession(doc);
                        session.name = tabName;
                        env.editor.focus();
            }

回答by sparkyspider

I use JQuery with the Ace Editor, and I found the following code worked really nicely for me. PS: My Code Editor Window is in an Iframe:

我将 JQuery 与 Ace 编辑器一起使用,我发现以下代码对我来说非常有用。PS:我的代码编辑器窗口在 Iframe 中:

$('#modelFrame').mouseover(function() {
    try {
        $(this).get(0).contentWindow.editor.focus();
    } catch (doNothing) {
        ;;
    }
});