<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>微博输入框140字限制</title>
<style>
*{margin:0;padding:0;}
body{font-size:12px;color:#666;}
em{font:normal 30px/1em Constantia,Georgia;}
textarea{width:600px;height:80px;padding:5px;line-height:18px;color:#999;overflow-y:auto;border:1px solid #333;resize:none}
/*resize:none 可防止FF下鼠标拉动放大textarea*/
.f_red{color:#f00}
</style>
<script src="jquery-1.4.2.min.js"></script>
</head>
<body>
<div>
<p id="wbLimitWord">请文明发言,还可以输入<em >140</em>字</p>
<p><textarea onKeyUp="writeWbTxt(this)" onChange="writeWbTxt(this)"></textarea></p>
</div>
<script>
function writeWbTxt(obj){
var num=checkWbTxt(obj)
if(num>=0){
$('#wbLimitWord').removeClass('f_red').html('请文明发言,还可以输入<em >'+num+'</em>字');
}else{
$('#wbLimitWord').addClass('f_red').html('已超出<em>'+Math.abs(num)+'</em>字');
}
}
function checkWbTxt(obj){
var val=obj.value;
var len=val.length;
//汉字和全角占两个字符
var addLen=(val.match(/[^\x00-\xff]|[\u4E00-\u9FA5]/g)||'').length;
var num=140-Math.ceil((len+addLen)/2);
return num;
}
</script>
</body>
</html>