聚焦输入框加载 [英] Focus Input Box On Load

查看:113
本文介绍了聚焦输入框加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在页面加载时将光标对准特定的输入框?



是否可以保留初始的文本值,并将光标置于输入的末尾?

 < input type =textsize =25id =myinputboxclass =input-text name =input2value =initial text/> 


解决方案

您的问题有两个部分。 >

1)如何在页面加载上集中输入?



您可以添加 autofocus 属性到输入。

 < input id =myinputboxtype =textautofocus> 

但是,所有浏览器可能不支持此功能,因此我们可以使用javascript。

  window.onload = function(){
var input = document.getElementById(myinputbox)。
}

2)如何将光标放在输入文本的末尾?



这是一个非jQuery解决方案,一些借用的代码来自另一个SO答案

  function placeCursorAtEnd(){
if(this.setSelectionRange){
//双倍的长度, Opera对于
//是否不一致,回车是否为一个字符或两个。
var len = this.value.length * 2;
this.setSelectionRange(len,len);
} else {
//这可能适用于没有setSelectionRange支持的浏览器。
this.value = this.value;
}

if(this.nodeName ===TEXTAREA){
//如果需要,这将滚动一个textarea到底部
this.scrollTop = 999999;
}
};

window.onload = function(){
var input = document.getElementById(myinputbox);

if(obj.addEventListener){
obj.addEventListener(focus,placeCursorAtEnd,false);
} else if(obj.attachEvent){
obj.attachEvent('onfocus',placeCursorAtEnd);
}

input.focus();
}

这里有一个我将如何用jQuery完成这个的例子。

 < input type =textautofocus> 

< script>
$(function(){
$([autofocus])。on(focus,function(){
if(this.setSelectionRange){
var len = this.value.length * 2;
this.setSelectionRange(len,len);
} else {
this.value = this.value;
}
this.scrollTop = 999999;
})。focus();
});
< / script>


How can the cursor be focus on a specific input box on page load?

Is it posible to retain initial text value as well and place cursor at end of input?

<input type="text"  size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />

解决方案

There are two parts to your question.

1) How to focus an input on page load?

You can just add the autofocus attribute to the input.

<input id="myinputbox" type="text" autofocus>

However, this might not be supported in all browsers, so we can use javascript.

window.onload = function() {
  var input = document.getElementById("myinputbox").focus();
}

2) How to place cursor at the end of the input text?

Here's a non-jQuery solution with some borrowed code from another SO answer.

function placeCursorAtEnd() {
  if (this.setSelectionRange) {
    // Double the length because Opera is inconsistent about 
    // whether a carriage return is one character or two.
    var len = this.value.length * 2;
    this.setSelectionRange(len, len);
  } else {
    // This might work for browsers without setSelectionRange support.
    this.value = this.value;
  }

  if (this.nodeName === "TEXTAREA") {
    // This will scroll a textarea to the bottom if needed
    this.scrollTop = 999999;
  }
};

window.onload = function() {
  var input = document.getElementById("myinputbox");

  if (obj.addEventListener) {
    obj.addEventListener("focus", placeCursorAtEnd, false);
  } else if (obj.attachEvent) {
    obj.attachEvent('onfocus', placeCursorAtEnd);
  }

  input.focus();
}

Here's an example of how I would accomplish this with jQuery.

<input type="text" autofocus>

<script>
$(function() {
  $("[autofocus]").on("focus", function() {
    if (this.setSelectionRange) {
      var len = this.value.length * 2;
      this.setSelectionRange(len, len);
    } else {
      this.value = this.value;
    }
    this.scrollTop = 999999;
  }).focus();
});
</script>

这篇关于聚焦输入框加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆