admin管理员组文章数量:1323723
This seems so simple, but it's not working:
document.getElementById('test').setSelectionRange(3,3)
<div id="test">test</div>
This seems so simple, but it's not working:
document.getElementById('test').setSelectionRange(3,3)
<div id="test">test</div>
Google Chrome.
Share Improve this question asked Aug 23, 2018 at 21:11 Phillip SennPhillip Senn 47.7k91 gold badges261 silver badges378 bronze badges3 Answers
Reset to default 2<input id="test" value="hello world" />
var InputElement = $('test')[0] as HTMLInputElement;
InputElement.setSelectionRange(3, 3);
This is because the 'test' element needs to be an <input/>
element. Try revising your HTML as follows:
<input id="test" />
Also consider the following changes to your javascript. With these updates to your code, your javascript should work as expected when there is enough text in your input field. For example, consider this code for selecting "world" in "hello world":
const input = document.getElementById('test');
input.focus();
input.setSelectionRange(6,11);
<input id="test" value="hello world" />
For more information, see the MDN docs for setSelectionRange()
To make this on <div>
you should make it editable:
<div id="test" contenteditable="true">test</div>
本文标签: javascriptsetSelectionRange is not a functionStack Overflow
版权声明:本文标题:javascript - setSelectionRange is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122534a2421785.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论