当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。