输入字符限制
<div style="position:absolute; top:20%; left:30%;">
<textarea style=" width:200px; height:100px;" onpropertychange="onKeyDown(this,200)" ></textarea>
<br />
<span>已输入字符: </span>
<span>0/200</span>
</div>
<script language="javascript">
/*
*函数名: onKeyDown
*功能: 判断文本框输入长度,如果长度大于maxLength数字,span标签字体变为红色
*参数: obj-textarea对象(有子标签),maxLength-标签提示文字变色最大长度
*返回值: true-执行成功,false-执行失败
*/
function onKeyDown(obj,maxLength/*最大长度,超过提示错误信息*/)
{ //onpropertychange
if (!obj || !obj.parentElement )
{
//没有textarea或span出错则返回
alert("OBJECT NULL");
return false;
}
var parentObj = obj.parentElement;
var inputedLength = obj.value.length ;
var textNumLabel = inputedLength + "/" + maxLength;
parentObj.getElementsByTagName("span")[1].innerHTML = textNumLabel;
if (inputedLength <= maxLength)
{
parentObj.getElementsByTagName("span")[1].style.color = "000000";
}
else
{
parentObj.getElementsByTagName("span")[1].style.color = "ff0000";
}
return true;
}
</script>