大马资讯论坛 - 马来西亚中文资讯平台
标题:
JavaScript 限制textarea的输入长度(IE浏览器,Firefox,Chrome)
[打印本页]
作者:
资讯王
时间:
2009-12-11 11:08
标题:
JavaScript 限制textarea的输入长度(IE浏览器,Firefox,Chrome)
复制以下的代码,放进您的html,即可使用
<!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=255
<textarea maxlength="255" onkeyup="return isMaxLen(this)" onkeydown="return isMaxLen(this)" onchange="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);
alert("The maximum number of characters allowed is 255.");
}
}
</script>
</body>
</html>
复制代码
欢迎光临 大马资讯论坛 - 马来西亚中文资讯平台 (http://freeinfo.com.my/)
Powered by Discuz! X3.3