在没有JavaScript支持的情况下加载页面时,如何在文本框中放置光标(自动聚焦)?


Answers:


146

您可以在不支持javascript的情况下进行操作。
我们可以使用html5自动对焦属性
例如:

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

如果在文本字段中使用它(autofocus =“ autofocus”),则意味着页面加载时该文本字段将获得焦点。有关更多详细信息:http :
//www.hscripts.com/tutorials/html5/autofocus-attribute.html


6
适当的大小写实际上是JSX / React的autoFocus。
thadk '16

37

只需添加autofocus第一个输入或文本区域。

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

1
至少在Chromeautofocus中不起作用,但autofocus="autofocus"可以。
杰伊·沙利文


3
<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.

OP说“没有javascript”。
艾蒂安·米雷特

1

对于那些像我一样摆弄一些东西的人来说,是一个扩展。

以下工作(来自W3):

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

重要的是要注意,尽管这在CSS中不起作用。即您不能使用:

.first-input {
    autofocus:"autofocus"
}

至少对我没有用...


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.