HTML:代码
<div> <label class="label">200个字符:</label> <div class="textarea"> <textarea class="change-length" placeholder="请输入文字"></textarea> <p class="textarea js-show-length">还可以输入<i class="js-num">200</i>个字</p> </div> </div>
JS代码部分:
change_length_fun=function(obj,num){ $(obj).on('keyup',function(){ var len = $(obj).val(); $(obj).siblings(".js-show-length").find(".js-num").text(num-len.length); if(len.length>=num){ $(obj).siblings(".js-show-length").find(".js-num").text(0); $(obj).val(len.substring(0,num)); } }); change_length_fun(".js-change-length",200);