jquery中操作input的selectionStart,不报错,点击之后没反应,也不起作用呀?

`<html>
<script src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">

function test(){
    $('#test').selectionStart = 0;
    $('#test').selectionEnd = 2;
}
</script>

<body>

<input type="button" value="登录" onclick="test()">        
<input name="test" id="test" >

</body>

</html>`

捕获.PNG

阅读 4.9k
3 个回答

注意下你的对象, js对象和jq 对象别弄混了,selectionStart 是js原生方法, 你的$('#test') 是jq对象,肯定没效果的,因jq 对象没有, 转换下就好了!
$('#test')[0].selectionStart = 0;
$('#test')[0].selectionEnd = 2;
或者
$('#test').get(0).selectionStart = 0;
$('#test').get(0).selectionEnd = 2;

$('#test')[0].selectionStart = 0;
$('#test')[0].selectionEnd = 2;
或者
$('#test').get(0).selectionStart = 0;
$('#test').get(0).selectionEnd = 2;

推荐问题