Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果您有一个 HTMLinput元素,您如何检测其中选定文本的开始和结束位置的索引input?我尝试使用window.getSelection,但它似乎无法正常工作。我需要在活动中弄清楚这一点keydown。
input
window.getSelection
keydown
您可以使用selectionStartandselectionEnd属性来获取各个值的索引。
selectionStart
selectionEnd
var start = document.getElementById("myArea").selectionStart; var end = document.getElementById("myArea").selectionEnd; console.log(start); console.log(end);