通过JavaScript将光标放在输入文本元素中文本末尾的最佳方法(我假设最简单的方法)是什么?在焦点设置为元素之后?
我有3个文本框用于电话号码.当用户键入时,它会自动从一个文本框移动到下一个文本框.当用户按下退格键时,我可以将焦点移动到上一个文本框.问题是在IE中,焦点设置为文本框的开头.这是我的代码,在chrome中运行良好.
$('#AreaCode').live('keyup', function (event) {
if ($(this).val().length == $(this).attr("maxlength"))
$('#Prefix').focus();
});
$('#Prefix').live('keyup', function (event) {
if (event.keyCode == 8 && $(this).val().length == 0)
$('#AreaCode').focus();
if ($(this).val().length == $(this).attr("maxlength"))
$('#Number').focus();
});
$('#Number').live('keyup', function (event) {
if (event.keyCode == 8 && $(this).val().length == 0)
$('#Prefix').focus();
});
Run Code Online (Sandbox Code Playgroud)
向后退时,如何将焦点设置在内容的末尾?