Html 当页面在没有 javascript 支持的情况下加载时,如何将光标(自动焦点)放置在文本框中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18054459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to place the cursor (auto focus) in text box when a page gets loaded without javascript support?
提问by Thavamani Kasi
I have a form with some text fields,and I want to place the cursor(auto focus)on first text field of form when page gets loaded.
我有一个带有一些文本字段的表单,我想在页面加载时将光标(自动焦点)放在表单的第一个文本字段上。
I want to do it without using javascript.
我想在不使用 javascript 的情况下做到这一点。
回答by Thavamani
Ya its possible to do without support of javascript..
We can use html5 auto focus attribute
For Example:
雅它可能不支持javascript..
我们可以使用html5自动焦点属性
例如:
<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
如果在文本字段中使用它 (autofocus="autofocus") 意味着当页面加载时文本字段会获得焦点..更多详细信息:http:
//www.hscripts.com/tutorials/html5/autofocus-attribute.html
回答by Rajnikant Kakadiya
Just add autofocus
in first input or textarea.
只需添加autofocus
第一个输入或文本区域。
<input type="text" name="name" id="xax" autofocus="autofocus" />
回答by Vishal
This will work:
这将起作用:
OnLoad="document.myform.mytextfield.focus();"
回答by dewaz
<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.
回答by AER
An expansion for those who did a bit of fiddling around like I did.
对于那些像我一样摆弄东西的人来说,这是一个扩展。
The following work (from W3):
以下工作(来自 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:
重要的是要注意这在 CSS 中不起作用。即你不能使用:
.first-input {
autofocus:"autofocus"
}
At least it didn't work for me...
至少对我没用...
回答by Marisela Sainz
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.
有时您需要做的就是确保光标在文本框内:单击文本框,显示菜单时,单击“设置文本框格式”,然后单击“文本框”选项卡,最后修改所有内容四个边距(左、右、上和下),向下箭头直到每个边距上出现“0”。