HTML DOM 文字區域自動對焦屬性


HTML DOM 文字區域自動對焦屬性返回並修改網頁載入時文字區域是否應自動獲得焦點。

語法

以下為語法 −

1. 返回自動對焦

object.autofocus

2. 修改自動對焦

object.autofocus = true | false

讓我們來看一個 HTML DOM 文字區域自動對焦屬性的示例

示例

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
   }
</style>
<body>
<h1>DOM Textarea autofocus Property Demo</h1>
<textarea autofocus>Hi! I'm a text area element with some dummy text.</textarea>
<button onclick="set()" class="btn">Disable Autofocus</button>
<script>
   function set() {
      document.querySelector("textarea").autofocus = false;
   }
</script>
</body>
</html>

輸出

點選“停用自動對焦”按鈕即可停用文字區域元素的自動對焦。現在,游標將不可見

更新於:2019-9-20

132 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告