【问题标题】:Characters preceding the cursor [duplicate]光标前的字符[重复]
【发布时间】:2012-01-26 07:52:57
【问题描述】:

是否可以使用 keydown 事件将索引检索到光标所在的文本区域中?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    是的。


    textarea.addEventListener('keydown', function (){
        console.log(textarea.selectionEnd); //The position of the cursor. If something is selected, then the position of the end of the selection.
    }, false);
    

    此功能不适用于 IE。在 IE 中你必须使用document.createTextRange()。

    【讨论】:

    • 我知道它适用于 Firefox、Safari。它在 IE 中有效吗?
    • @Akhil 正如我在回答中所说,不,不会。
    【解决方案2】:

    请参阅以下链接以了解实施。您可以在 keydown 事件上调用 getCaret 函数来获取插入符号的索引:

    https://stackoverflow.com/a/263796/639960

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 2011-06-20
      相关资源
      最近更新 更多