Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setSelectionRange不是一个函数_Javascript_Selection - Fatal编程技术网

Javascript setSelectionRange不是一个函数

Javascript setSelectionRange不是一个函数,javascript,selection,Javascript,Selection,这看起来很简单,但不起作用: document.getElementById('test').setSelectionRange(3,3) test这是因为“test”元素必须是元素。尝试按如下方式修改HTML: <input id="test" /> var InputElement=$('test')[0]作为HTMLInputElement; InputElement.setSelectionRange(3,3); 那么,如果我们想要它或,该怎么办?@AnilPBabu我

这看起来很简单,但不起作用:

document.getElementById('test').setSelectionRange(3,3)

test
这是因为“test”元素必须是
元素。尝试按如下方式修改HTML:

<input id="test" />

var InputElement=$('test')[0]作为HTMLInputElement;
InputElement.setSelectionRange(3,3);

那么,如果我们想要它或,该怎么办?@AnilPBabu我不确定我是否明白-你能详细告诉我你的意思吗?你说“这是因为‘测试’元素需要是一个元素。”。但这个问题是为了解决这个问题。有什么方法可以应用setSelectionRange for我不知道有什么方法可以应用于
sorryThink我们需要将元素作为一个数组索引为1$('test')[0],然后调用setSelectionRange(3,3)。示例:$('test')[0].setSelectionRange(3,3);
<input id="test" value="hello world" />

var InputElement = $('test')[0] as HTMLInputElement;
InputElement.setSelectionRange(3, 3);