HTML DOM 選擇自動聚焦屬性


HTML DOM 選擇自動聚焦屬性可返回並修改頁面載入時下拉列表是否獲得焦點。

語法

以下是語法 -

  • 1. 返回自動聚焦

object.autofocus
  • 修改自動聚焦

object.autofocus = true | false

示例

讓我們看一個 HTML DOM 選擇自動聚焦屬性的示例 -

 線上演示

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM autofocus property</title>
<style>
   body{
      text-align:center;
      color:#fff;
      background: radial-gradient( circle farthest-corner at 23.1% 64.6%, rgba(129,125,254,1) 0%, rgba(111,167,254,1) 90% ) no-repeat;
      height:100vh;
   }
   p{
      font-weight:700;
      font-size:1.1rem;
   }
   .drop-down{
      display:block;
      width:35%;
      border:none;
      font-weight:bold;
      padding:10px;
      margin:1rem auto;
      background-color:#ffffff7a;
      outline-color:black;
   }
   .btn{
      background:orange;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      font-weight:bold;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>autofocus Property Example</h1>
<p>Hi, Select your favourite subject:</p>
<select class='drop-down' name="Drop Down List">
<option>Physics</option>
<option>Maths</option>
<option>Chemistry</option>
<option>English</option>
<option>Economics</option>
<option>Hindi</option>
<option>Biology</option>
</select>
<br>
<button onclick="disable()" class="btn">Disable Autofocus</button>
<script>
   function disable() {
      document.querySelector(".drop-down").autofocus = false;
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 -

點選“停用自動聚焦”按鈕以停用下拉列表上的自動聚焦。

更新於: 01-Jul-2020

76 瀏覽量

開啟 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.