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


HTML DOM Keygen autofocus屬性用法及代碼示例

HTML DOM 中的 keygen autofocus 屬性用於設置或返回 <keygen> 元素的 autofocus 屬性的值。 autofocus 屬性用於定義 keygen 元素是否在頁麵加載或刷新時自動獲得焦點。

用法:

返回自動對焦屬性。

keygenObject.autofocus

設置自動對焦屬性。



keygenObject.autofocus = true|false

屬性值:

  • 真|假:它用於指定在頁麵加載或刷新時 keygen 元素是否應該獲得焦點。默認為假。

返回值:它返回一個布爾值,表示 keygen 元素是否必須聚焦。

範例1:下麵的示例說明了如何返回自動對焦屬性。

HTML


<!DOCTYPE html>
<html>
   
<head>
    <style>
        h1 {
            color:green;
        }
         
        h2 {
            font-family:Impact;
        }
         
        body {
            text-align:center;
        }
    </style>
</head>
   
<body>
    <h1>GeeksforGeeks</h1>
    <h2>HTML DOM Keygen autofocus Property</h2>
    <br>
    <form id="myGeeks">
        Username:<input type="text" name="uname">
        <br><br> Encryption:
        <keygen id = "Geeks"form="myGeeks"
                name="secure" autofocus>
        <input type="submit">
    </form>
     
<p>
          To find out whether the keygen element
        automatically gets focus on page load
          or ot, click the "Check" button.
      </p>
    <button onclick="My_focus()">Check</button>
    <p id="test"></p>
   
    <script>
        function My_focus() {
            var d = document.getElementById("Geeks").autofocus;
            document.getElementById("test").innerHTML = d;
        }
    </script>
</body>
   
</html>

輸出:

範例2:下麵的示例演示如何設置自動對焦屬性。

HTML


<!DOCTYPE html>
<html>
<head>
    <style>
        h1 {
            color:green;
        }
        h2 {
            font-family:Impact;
        }
        body {
            text-align:center;
        }
    </style>
</head>
<body>
    <h1>GeeksforGeeks</h1>
    <h2>HTML DOM Keygen autofocus Property</h2>
    <br>
    <form id="myGeeks">
        Username:<input type="text" name="uname">
        <br><br> Encryption:
        <keygen id="Geeks" form="myGeeks"
            name="secure" autofocus>
        <input type="submit">
    </form>
     
     
<p>
        To set the autofocus property,
        click on "set" Button.
    </p>
    
    <button onclick="set_focus()">set</button>
     
    <p id="test"></p>
     
    <script>
        function set_focus() {
            var d = document.getElementById(
                    "Geeks").autofocus = "false";
                     
            document.getElementById("test").innerHTML = d;
        }
    </script>
</body>
</html>

輸出:




相關用法


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