41

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

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

请问有人可以帮忙吗?

4

5 回答 5

37
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
于 2012-03-22T08:34:47.790 回答
22

聚焦到最后:

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

感谢@a-user

于 2017-05-29T12:39:59.860 回答
11

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

//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);
于 2013-03-12T23:33:20.400 回答
3

这是我的代码的摘录,它将焦点设置在 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();
            }
于 2011-08-15T16:47:26.880 回答
0

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

$('#modelFrame').mouseover(function() {
    try {
        $(this).get(0).contentWindow.editor.focus();
    } catch (doNothing) {
        ;;
    }
});
于 2011-08-22T09:12:48.487 回答