【问题标题】:How to move the cursor to the end of the line in ACE Editor?如何在 ACE Editor 中将光标移动到行尾?
【发布时间】:2014-12-23 17:15:06
【问题描述】:

在我的 Javascript 文件中,我有以下代码行:

editor.focus(); // To focus the ace editor
var n = editor.getSession().getValue().split("\n").length - 2;
editor.gotoLine(n); 

这会聚焦 ACE 编辑器,移动到最后一行,然后向上移动两行。当它上升这两行时,它也会将光标移动到该行的前面。模拟某人按下 Home 键。

问:如何将光标移动到行尾?还是模拟End键?

【问题讨论】:

    标签: javascript html ace-editor


    【解决方案1】:

    gotoline 接受两个参数,一个用于行,一个用于列

    var row = editor.session.getLength() - 1
    var column = editor.session.getLine(row).length // or simply Infinity
    editor.gotoLine(row + 1, column)
    

    请注意,gotoLine 会通过动画将所选内容滚动到视图中,如果您不想这样做,可以使用

    editor.selection.moveTo(row, column)
    

    改为。

    要完全按照用户按下 end 时的处理方式来模拟 end 键,请使用

    editor.execCommand("gotolineend")
    

    【讨论】:

    • 第一个gotoLine 有错别字。其他的拼写正确。
    【解决方案2】:

    直接来自API

    您想使用方法navigateLineEnd()。所以在你的情况下:

    editor.focus(); // To focus the ace editor
    var n = editor.getSession().getValue().split("\n").length - 2;
    editor.gotoLine(n); 
    editor.navigateLineEnd() // Navigate to end of line
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多