xiaocaibai 2014-12-19
<!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="枫岩,[email protected]"> <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>
textarea标签是我们经常使用的一个html标签,主要是在输入较长的文本时,可以实现换行等效果。下面引用一个他的基本使用:。<textarea cols="50" rows="10" id="co