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


HTML Textarea maxlength用法及代碼示例


DOM Textarea maxlength屬性用於設置或返回textarea字段的maxlength屬性值。它指定元素中允許的最大字符數。

用法:

  • 它用於返回maxLength屬性:
    textareaObject.maxLength
  • 它用於設置maxLength屬性:
    textareaObject.maxLength = number

屬性值


  • number:它指定Textarea元素中允許的最大字符數。

返回值:它返回一個數字值,該值表示Textarea字段中允許的最大字符數。

示例1:HTML程序,說明如何返回maxlength屬性。

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green; 
                   font-style:italic;"> 
          GeeksforGeeks 
      </h1> 
        
        <h2 style="color:green; 
                   font-style:italic;"> 
          DOM Textarea maxlength Property 
      </h2> 
        
        <textarea rows="4" 
                  cols="50"
                  id="GFG" 
                  maxlength="6"> 
          write here.... 
      </textarea> 
        <br> 
        <br> 
        
        <button onclick="myGeeks()"> 
          Submit 
      </button> 
  
        <p id="sudo"></p> 
  
        <script> 
            function myGeeks() { 
                
             // Return max length allowed in the textarea field. 
             var x = document.getElementById("GFG").maxLength; 
                
             document.getElementById("sudo").innerHTML = 
             "There are only " + x + " maximum characters" + 
             "are allowed in a Textarea Field.";; 
            } 
        </script> 
</body> 
  
</html>

輸出:

在單擊按鈕之前:

單擊按鈕後:

示例2:HTML程序說明如何設置maxlength屬性。

   
<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green; 
                   font-style:italic;"> 
          GeeksforGeeks 
      </h1> 
        
        <h2 style="color:green; 
                   font-style:italic;"> 
          DOM Textarea maxlength Property 
      </h2> 
        
      <textarea rows="4" 
                cols="50" 
                id="GFG"
                maxlength="6"> 
        write here.... 
      </textarea> 
      <br> 
      <br> 
        
      <button onclick="myGeeks()"> 
        Submit 
      </button> 
  
        <p id="sudo"></p> 
  
        <script> 
            function myGeeks() { 
                
                // Set maxlength 4. 
                var x = 
                document.getElementById("GFG").maxLength = "4" 
                  
                document.getElementById("sudo").innerHTML = 
                  "Maximum number of characters allowed"+ 
                "in the text area is now 4."; 
                "4" 
            } 
        </script> 
  </center> 
</body> 
  
</html>

輸出:

在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了Textarea maxLength屬性支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • Opera
  • Safari


相關用法


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