类似微博的字数统计功能,代码如下:
<script language="javascript"> function countChar(textareaName,spanName) { document.getElementById(spanName).innerHTML = 140 - document.getElementById(textareaName).value.length; } </script> 还可以输入<span id="counter">140</span>字<br/> <textarea id="status" name="status" rows="6" cols="40" onkeydown='countChar("status","counter");' onkeyup='countChar("status","counter");'></textarea>
另外一种实现方法:
<script> function title_len(){ var value = $('#news_title').val().length; if(value == 80){ var string = "<span>"+value+"/80</span>"; }else{ var string = "<span>"+value+"</span>/80"; } $('#titlelen').html(string); } </script>