【发布时间】:2012-01-26 07:52:57
【问题描述】:
是否可以使用 keydown 事件将索引检索到光标所在的文本区域中?
【问题讨论】:
标签: javascript
是否可以使用 keydown 事件将索引检索到光标所在的文本区域中?
【问题讨论】:
标签: javascript
是的。
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()。
【讨论】:
请参阅以下链接以了解实施。您可以在 keydown 事件上调用 getCaret 函数来获取插入符号的索引:
【讨论】: