HTML


<button> 元素的 autofocus 屬性用於在頁面載入時將焦點設定到按鈕上。

以下是語法 −

<button type="button" autofocus>

以上,我們已經將 autofocus 設定為某個按鈕。現在讓我們來看一個示例來實現 <button> 元素的 autofocus 屬性 −

示例

 線上演示

<!DOCTYPE html>
<html>
<body>
   <h2>Demo Heading</h2>
   <p>This is a demo line.</p>
   <button type="button" autofocus >Demo</button>
</body>
</html>

輸出

這將產生以下輸出,顯示焦點在按鈕 1 上 −

在以上示例中,我們為按鈕元素設定了 autofocus 屬性 −

<button type="button" autofocus >
   Demo
</button>

現在,當頁面載入時,焦點將放在按鈕本身上,並且訪客將在網頁上看到這一點。

更新時間: 29-6 月 2020

222 次瀏覽

開啟你的 職業生涯

完成課程認證

入門
廣告
© . All rights reserved.