专注于有价值的输入字段 [英] Focus on input field with value
本文介绍了专注于有价值的输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我专注于使用jQuery的输入字段:
I am focusing on an input field with jQuery:
$("input:text").focus();
输入字段中已有一些文本值。当我聚焦时,光标在最后一个字母后面闪烁,我如何将光标放在第一个字母的前面?
There is already some text value in the input field. When I focus, the cursor blinks right after the last letter, how would I put the cursor right in front of the first letter?
推荐答案
<你可以使用我为你创建的这个小插件(修改自此脚本):
jQuery.fn.setCaret = function (pos) {
var input = this[0];
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(pos, pos);
} else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
};
// usage:
$('input:text').setCaret(0);
这篇关于专注于有价值的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文