HTML DOM 文字區域 readOnly 屬性


HTML DOM 文字區域 readOnly 屬性返回並修改在 HTML 文件中文字區域元素的內容是否應為只讀。

語法

以下為語法 −

1. 讀取 readOnly

object.readOnly

2. 新增 readOnly

object.readOnly = true | false

讓我們來看一個 HTML DOM 文字區域 readOnly 屬性的示例 −

示例

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      background: lightseagreen;
      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 readOnly Property Demo</h1>
<textarea rows="5" cols='20' name="I'm name attribute of textarea element">
Hi! I'm a text area element with some dummy text.
</textarea>
<button onclick="set()" class="btn">Toggle Read Only</button>
<script>
   function set() {
      var ta = document.querySelector("textarea");
      if (ta.readOnly === true) {
         ta.readOnly = false;
      } else {
         ta.readOnly = true;
      }
   }
</script>
</body>
</html>

輸出

單擊“切換隻讀”按鈕可切換文字區域元素的 readOnly 屬性值。

更新於:20-6 月-2020

170 次瀏覽

開啟您的 職業

完成課程後獲得認證

開始
廣告
© . All rights reserved.