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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744917381a4600916.html
评论列表(0条)