繁体   English   中英

焦点输入框在负载下

[英]Focus Input Box On Load

如何在页面加载时将光标聚焦在特定的输入框上?

是否还可以保留初始文本值并将光标放在输入的末尾?

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

您的问题分为两部分。

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

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

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

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

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

2)如何将光标置于输入文本的末尾?

这是一个非jQuery解决方案,其中的一些代码是从另一个SO答案中借来的。

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();
}

这是我如何使用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>

请注意-您现在可以在不支持JavaScript的HTML5上针对支持它的浏览器执行此操作:

<input type="text" autofocus>

您可能想从此开始,并使用JavaScript构建它,以为较旧的浏览器提供后备功能。

$(document).ready(function() {
    $('#id').focus();
});
function focusOnMyInputBox(){                                 
    document.getElementById("myinputbox").focus();
}

<body onLoad="focusOnMyInputBox();">

<input type="text"  size="25" id="myinputbox" class="input-text" name="input2" onfocus="this.value = this.value;" value = "initial text">

这样做的一个可移植的方法是使用自定义函数(处理浏览器的差异),像这一个

然后,如jessegavin所写,在<body>标记的末尾为onload设置一个处理程序:

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

工作正常...

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

非常简单的一线解决方案:

<body onLoad="document.getElementById('myinputbox').focus();">

这对我来说很好:

<form name="f" action="/search">
    <input name="q" onfocus="fff=1" />
</form>

fff将是一个全局变量,其名称绝对无关紧要,其目的是停止通用的onload事件,以强制将焦点集中在该输入上。

<body onload="if(!this.fff)document.f.q.focus();">
    <!-- ... the rest of the page ... -->
</body>

来自: http : //webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html

如果由于某种原因而无法添加到BODY标签,则可以在表单之后添加此标签:

<SCRIPT type="text/javascript">
    document.yourFormName.yourFieldName.focus();
</SCRIPT>

尝试:

Javascript Pure:

[elem][n].style.visibility='visible';
[elem][n].focus();

jQuery:

[elem].filter(':visible').focus();

将此添加到您的js顶部

var input = $('#myinputbox');

input.focus();

或HTML

<script>
    var input = $('#myinputbox');

    input.focus();
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM