完美解决textarea字数限制

  1. <textarea id="area" name="ss" placeholder="请输入文本内容"></textarea>  
  2. <p><span id="text-count">20</span>/20</p>  
  3. <script type="text/javascript">  
  4.     /*字数限制*/  
  5.     $("#area").on("input propertychange", function() {  
  6.         var $this = $(this),  
  7.             _val = $this.val(),  
  8.             count = "";  
  9.         if (_val.length > 20) {  
  10.             $this.val(_val.substring(0, 20));  
  11.         }  
  12.         count = 20 - $this.val().length;  
  13.         $("#text-count").text(count);  
  14.     });  
  15. </script>  

1、input、textarea都有maxlength属性,但是textarea不兼容ie8/9,input兼容ie8/9。

2、同时绑定onchange、onkeydown、onkeyup,ie8/9下解决不了右键粘贴问题。

福建C# .net  技术群

原文地址:https://www.cnblogs.com/annkiny/p/8566687.html