setSelectionRange()函数-只支持Firefox浏览器-设置文本框某段文字的光标选中

例如页面上有一个文本输入框:

<input type="text" id="txt" value="abcdefghijklmn"/>

则,设置该文本框中的最后三个字符光标选中:

var o = document.getElementById("txt");

var len = o.value.length;

o.focus(); //先使光标聚集在输入框中

o.setSelectionRange(len-3,len);

总结:

使用格式:o.setSelectionRange(start,end);

o:为文本输入框对象

start:为字符串的起始位置

end:为字符串的末位置

加支付宝好友偷能量挖...


评论(0)网络
阅读(140)喜欢(0)JavaScript/Ajax开发技巧