html 对文字字数限制的jquery代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text ml; charset=utf-8" />
<title>无标题文档</title>
<body>
<div id = "main"></div>
    <div class="inputDiv" style=position:absolute; left:30%;>
     <textarea style= width:200px; height:100px; onfocus="checkInputedLegal(this,200)"></textarea>
     <br />
        <span>0/200</span>
    </div>
</body>

<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
/*
*函数名:checkInputedLegal
*功能:检查输入字符,如果字符输入大于maxLength统计字符变红
*参数:obj-textarea对象,maxLength-允许最大输入长度
*返回值:true-执行成功,false-执行失败
*/
function checkInputedLegal(obj,maxLength)
{
$(obj).bind('keyup keydown',function()
{
  var content_len = $(this).val().length;
var labelSpan = $(this).parent().find('span');
 
if (!labelSpan)
{
alert("null object");
return false; 

labelSpan.html(content_len+'/' + maxLength );

//设置字体颜色
if( content_len <= maxLength )
{
//输入合法
labelSpan.css("color","#000000");
}
else
{
//输入超限
labelSpan.css("color","#ff0000");
}
});
return true;
}
</script>
</head>

<html>

posted @ 2012-05-23 14:04  lcuzhanglei  阅读(1038)  评论(0编辑  收藏  举报