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


HTML Input URL autofocus用法及代碼示例


DOM輸入URL自動聚焦屬性用於設置或返回頁麵加載時輸入URL字段是否應獲得焦點。此屬性用於反映HTML自動對焦屬性。

用法:

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

屬性值:


  • true:它定義了URL字段獲得焦點。
  • false:它具有默認值。它定義了URL字段不被關注。

返回值:它返回一個布爾值,該值表示URL字段是否自動聚焦。

示例1:本示例說明了如何返回屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        DOM Input URL autofocus Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1> 
  
        <h2> 
          DOM Input URL autofocus Property 
      </h2> 
  
        <label for="uname"
               style="color:green"> 
      </label> 
  
            <form id="geeks"> 
                <input type="url"
                       id="gfg"
                       placeholder="Enter URL" 
                       size="20" 
                       value="GeeksForGeeks"
                       pattern="https?://.+"
                       title="Include http://" 
                       maxlength="20" 
                       autofocus> 
            </form> 
            <br> 
            <br> 
  
            <button type="button" 
                    onclick="geeks()"> 
                Click 
            </button> 
  
            <p id="GFG"
               style="color:green; 
                      font-size:25px;"> 
          </p> 
  
            <script> 
                function geeks() { 
                    
                    var link =  
                        document.getElementById( 
                          "gfg").autofocus; 
                    
                    document.getElementById( 
                      "GFG").innerHTML = link; 
                } 
            </script> 
    </center> 
</body> 
  
</html>

輸出
在單擊按鈕之前:

單擊按鈕後:

示例2:本示例說明如何設置屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        DOM Input URL autofocus Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1> 
  
        <h2> 
          DOM Input URL autofocus Property 
      </h2> 
  
        <label for="uname" 
               style="color:green"> 
      </label> 
  
            <form id="geeks"> 
                <input type="url" 
                       id="gfg"
                       placeholder="Enter URL" 
                       size="20"
                       value="GeeksForGeeks" 
                       pattern="https?://.+" 
                       title="Include http://" 
                       maxlength="20" 
                       autofocus> 
            </form> 
            <br> 
            <br> 
  
            <button type="button" 
                    onclick="geeks()"> 
                Click 
            </button> 
  
            <p id="GFG" 
               style="color:green; 
                      font-size:25px;"> 
      </p> 
  
            <script> 
                function geeks() { 
                    
                    var link =  
                        document.getElementById( 
                          "gfg").autofocus = "false"; 
                    
                    document.getElementById( 
                      "GFG").innerHTML = link; 
                } 
            </script> 
    </center> 
</body> 
  
</html>

輸出:
在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:DOM輸入URL自動聚焦屬性支持的瀏覽器如下:

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


相關用法


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