###原文地址:https://blog.csdn.net/fb_01/article/details/51026774
- input、textarea都有maxlength属性,但是textarea不兼容ie8/9,input兼容ie8/9。
- 同时绑定onchange、onkeydown、onkeyup,ie8/9下解决不了右键粘贴问题。
- 具体代码如下:
<textarea name="detail" id="detail" cols="" rows="" placeholder="请输入内容" style="height: 250px;"></textarea><p><span id="text-count">255</span>/255</p><script type="text/javascript">$('#detail').on("input propertychange",function(){var this = $(this);_val = $this.val(),count = "";if(_val.length > 255){$this->val(_val.substring(0,255));}count = 255 - $this.val().length;$("#text-count").text(count);});</script>