admin管理员组文章数量:1405516
How do some sites automatically select a input field when you enter a page? Like YouTube's login page it selects the Username field automatically
This site too, on page Ask Question for example, it selects the Title field.
How do they do this? My guess would be javascript
But how?
How do some sites automatically select a input field when you enter a page? Like YouTube's login page it selects the Username field automatically
This site too, on page Ask Question for example, it selects the Title field.
How do they do this? My guess would be javascript
But how?
Share edited Aug 31, 2009 at 1:48 vmarquez 1,37711 silver badges21 bronze badges asked Aug 31, 2009 at 1:43 Daniel MoralesDaniel Morales 1- 1 @Daniel Moralea: Just a note, the action you are referring to is known as focus. Selection is a different beast. – vmarquez Commented Aug 31, 2009 at 1:51
4 Answers
Reset to default 2<head>
<!-- set focus to a field with the name "searchcontent" in my form -->
<script type="text/javascript">
function setfocus(a_field_id) {
$(a_field_id).focus()
}
</script>
</head>
<body onload="setfocus('customervalue');">
Customer: <input name="customer" id="customervalue" />
</body>
From here: http://lena.franken.de/software/javascript/index.html
In The Future: <input type="text" autofocus>
:-)
Not a good idea to use onload on body either, use an onDomReady-event, for instance from YUI or jQuery or some custom script instead.
Use:
someObject.focus()
Reference:
http://www.w3schools./HTMLDOM/met_anchor_focus.asp
Most sites currently do this with JavaScript autofocusers, as given in other answers here. But if you're thinking of implementing similar behaviour, be aware that this can be annoying for some users due to the way the JavaScript works. For example, if you focus a different field while a page is still loading, the JavaScript may "help" you by moving the focus and making you type in the wrong field. This question on ui.stackexchange. enumerates some more drawbacks.
To prevent this, HTML5 has introduced an autofocus
attribute for form elements that means the behaviour will be consistently implemented by the browser itself, and can be turned off for users that find it irritating. Of course, this isn't supported by all browsers yet. For more info, see the section on autofocus fields in Mark Pilgrim's excellent Dive Into HTML5 book.
本文标签: javascriptHow do I auto select an input field when I load a pageStack Overflow
版权声明:本文标题:javascript - How do I auto select an input field when I load a page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744917380a2632076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论