当一个页面在没有 javascript 支持的情况下加载时,如何将光标(自动焦点)放在文本框中?

我有一个带有一些文本字段的表单,我想在页面加载时将 cursor(自动对焦)放在表单的第一个文本字段上。

我不想使用 javascript。

198210 次浏览

This will work:

OnLoad="document.myform.mytextfield.focus();"

Ya its possible to do without support of javascript..
We can use html5 auto focus attribute
For Example:

<input type="text" name="name" autofocus="autofocus" id="xax" />

If use it (autofocus="autofocus") in text field means that text field get focused when page gets loaded.. For more details:
http://www.hscripts.com/tutorials/html5/autofocus-attribute.html

Just add autofocus in first input or textarea.

<input type="text" name="name" id="xax" autofocus="autofocus" />
<body onLoad="self.focus();document.formname.name.focus()" >


formname is <form action="xxx.php" method="POST" name="formname" >
and name is <input type="text" tabindex="1" name="name" />


it works for me, checked using IE and mozilla.
autofocus, somehow didn't work for me.

Sometimes all you have to do to make sure the cursor is inside the text box is: click on the text box and when a menu is displayed, click on "Format text box" then click on the "text box" tab and finally modify all four margins (left, right, upper and bottom) by arrowing down until "0" appear on each margin.

An expansion for those who did a bit of fiddling around like I did.

The following work (from W3):

<input type="text" autofocus />
<input type="text" autofocus="" />
<input type="text" autofocus="autofocus" />
<input type="text" autofocus="AuToFoCuS" />

It is important to note that this does not work in CSS though. I.e. you can't use:

.first-input {
autofocus:"autofocus"
}

At least it didn't work for me...

very easy you can just set the attribute autofocus to on in the wanted input

<form action="exemple.php"  method="post">
<input name="wantedInput" autofocus="on">
<input type="submit" value="go"  >
</form>