当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。