HTML DOM Textarea maxLength 屬性


HTML DOM Textarea maxLength 屬性返回並修改 HTML 文件中 textarea 元素的 maxLength 屬性的值。

語法

以下是語法 −

1. 返回 maxLength

object.maxLength

2. 修改 maxLength

object.maxLength = “number”

讓我們看一個 HTML DOM Textarea maxLength 屬性的示例

示例

即時演示

<!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;
   }
   .show {
      font-size: 1.5rem;
      margin: 1rem 0;
   }
</style>
<body>
<h1>DOM Textarea maxLength Property Demo</h1>
<textarea maxlength="50">Hi! I'm a text area element with some dummy text.</textarea>
<button onclick="set()" class="btn">Show Maximum Length</button>
<div class="show"></div>
<script>
   function set() {
      document.querySelector('.show').innerHTML = 'The maximum number of characters allowed in the above text area is ' + document.querySelector("textarea").maxLength;
   }
</script>
</body>
</html>

輸出

點選“顯示最大長度”按鈕以顯示 textarea 元素的 maxLength 屬性的值。

更新於: 16-2 月-2021

149 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.