當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


HTML Input Button autofocus用法及代碼示例

HTML DOM中的DOM Input Button autofocus屬性用於設置或返回在頁麵加載時Input Button字段是否應自動獲得焦點。此屬性用於反映HTML自動對焦屬性。

用法:

  • 它返回自動對焦屬性。
    buttonObject.autofocus
  • 用於設置自動對焦屬性。
    buttonObject.autofocus = true|false

屬性值:


  • true:指定“按鈕”字段獲得焦點。
  • false:它指定“按鈕”字段不獲得焦點。它具有默認值。

返回值:它返回一個布爾值,該值表示頁麵加載時Button字段是否獲得焦點。

示例1:本示例說明了如何返回自動對焦屬性。

<!DOCTYPE html>  
<html>  
  
<head>  
    <style>  
        h1 {  
            color:green;  
        }  
    </style>  
</head>  
  
<body style="text-align:center;">  
    <h1>GeeksForGeeks</h1>  
    <h2> DOM Input Button autofocus Property </h2>  
        <form id="myGeeks"> 
    <!-- Assigning button id -->
    <input type="button" id="GFG" onclick="myGeeks()" 
    name="Geek_button" value="Submit" autofocus>  
                    </form> 
    <p id="sudo" style="color:green;font-size:25px;"></p>  
  
    <script>  
        function myGeeks() {  
              
            // accessing 'button' id.  
            var g = document.getElementById("GFG").autofocus;  
            document.getElementById("sudo").innerHTML = g;  
        }  
    </script>  
  
</body>  
  
</html> 

輸出:

在單擊按鈕之前:

單擊按鈕後:

示例2:本示例說明了如何設置自動對焦屬性。

<!DOCTYPE html>  
<html>  
  
<head>  
    <style>  
        h1 {  
            color:green;  
        }  
    </style>  
</head>  
  
<body style="text-align:center;">  
    <h1>GeeksForGeeks</h1>  
    <h2> DOM Input Button autofocus Property </h2>  
        <form id="myGeeks"> 
    <!-- Assigning button id -->
    <input type="button" id="GFG" onclick="myGeeks()" 
     name="Geek_button" value="Submit" autofocus>  
                    </form> 
    <p id="sudo" style="color:green;font-size:25px;"></p>  
  
    <script>  
        function myGeeks() {  
              
            // accessing 'button' id.  
            var g = document.getElementById("GFG").autofocus = false;  
            document.getElementById("sudo").innerHTML = g;  
        }  
    </script>  
  
</body>  
  
</html> 

輸出:
在單擊按鈕之前:

單擊“打開”按鈕後:

支持的瀏覽器:下麵列出了DOM輸入Button自動對焦屬性支持的瀏覽器:

  • 穀歌瀏覽器
  • Internet Explorer 10.0以上
  • Firefox
  • Opera
  • Safari


相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 HTML | DOM Input Button autofocus Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。