HTMLautofocus 屬性


<input> 元素的 autofocus 屬性用於指定哪個 <input> 在網頁載入時獲取焦點。此屬性在 HTML5 中引入。

以下為語法 −

<input autofocus>

現在我們來看一個示例,以便實現 <input> 元素的 autofocus 屬性 −

示例

 動態演示

<!DOCTYPE html>
<html>
<body>
<h2>Log in to your account</h2>
<form action="" method="get">
   Id: <input type="text" name="id"><br>
   Password: <input type="password" name="pwd"><br>
   DOB: <input type="date" name="dob" autofocus><br>
   <button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>

輸出

在上面的示例中,我們建立了一個表單 −

<form action="" method="get">
   Id: <input type="text" name="id"><br>
   Password: <input type="password" name="pwd"><br>
   DOB: <input type="date" name="dob" autofocus><br>
   <button type="submit" value="Submit">Submit</button>
</form>

我們已將 autofocus 設定為第三個輸入,如下所示:DOB,因此游標在該處可見 −

DOB: <input type="date" name="dob" autofocus><br>

更新於: 2020 年 6 月 29 日

77 次瀏覽

啟動你的 職業生涯

透過完成課程進行認證

開始
廣告
© . All rights reserved.