HTML DOM Button autofocus 屬性
HTML DOM Button autofocus 屬性與 <button> 元素的 autofocus 屬性關聯。button autofocus 屬性用於指定 HTML 文件上的按鈕在頁面載入時是否應獲得焦點。
語法
以下是其語法:-
設定按鈕 autofocus 屬性 -
buttonObject.autofocus = true|false
此處,true|false 指定給定的輸入按鈕在頁面載入時是否應獲得焦點。
- **真** - 輸入按鈕獲得焦點
- **假** - 輸入按鈕不會獲得焦點。
示例
我們來看一個 HTML DOM Button autofocus 屬性的示例 -
<!DOCTYPE html>
<html>
<body>
<button type="button" id="MyButton" autofocus>BUTTON</button>
<p>Click the below button to know if the input button above automatically gets the focus on page load or not</p>
<button onclick="buttonFocus()">CLICK IT</button>
<p id="Sample"></p>
<script>
function buttonFocus() {
var x = document.getElementById("MyButton").autofocus;
if(x==true)
document.getElementById("Sample").innerHTML="The input button does get focus on page load";
else
document.getElementById("Sample").innerHTML="The input button does not get focus on
page load";
}
</script>
</body>
</html>輸出
這將產生以下輸出 -

單擊 CLICK IT 按鈕 -

在上述示例中 -
我們有一個 id 為“MyButton”並且啟用了 autofocus 屬性的按鈕 -
<button type="button" id="MyButton" autofocus>BUTTON</button>
然後,我們有一個 CLICK IT 按鈕來執行 buttonFocus() 函式 -
<button onclick="buttonFocus()">CLICK IT</button>
buttonFocus() 函式使用 getElementById() 方法獲取按鈕元素並獲取其 autofocus 值(為布林值),並將其分配給變數 x。我們使用條件語句檢查 autofocus 值是 true 還是 false,並根據 id“Sample”及相關內容在 <p> 元素中顯示相應的文字。
function buttonFocus() {
var x = document.getElementById("MyButton").autofocus;
if(x==true)
document.getElementById("Sample").innerHTML="The input button does get focus on page load";
else
document.getElementById("Sample").innerHTML="The input button does not get focus on page load";
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP