用JavaScript限制textarea输入长度 (For: IE、Firefox ...)

<!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" lang="gb2312">  
<head>  
<head>  
<title> Code:用JavaScript限制textarea输入长度 (For: IE、Firefox ...)</title>  
<meta http-equiv="content-type" content="text/html; charset=gb2312" />  
<meta name="author" content="枫岩,CNLei.y.l@gmail.com">  
<meta name="keywords" content="textarea,输入长度" />  
<meta name="description" content="用JavaScript限制textarea输入长度 (For: IE、Firefox ...)" />  
<style type="text/css" media="all">  
 body {font-size:14px;}  
</style>  
</head>  
<body>  
maxlength=10
  
<textarea maxlength="10" onkeyup="return isMaxLen(this)"></textarea>  


  
maxlength=20
  
<textarea maxlength="20" onkeyup="return isMaxLen(this)"></textarea>  

<script type="text/javascript">  
function isMaxLen(o){  
 var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";  
 if(o.getAttribute && o.value.length>nMaxLen){  
 o.value=o.value.substring(0,nMaxLen)  
 }  
}  
</script>  
</body>  
</html>

相关推荐