<html> <head> <title>JQuery为textarea添加maxlength</title> <script type="text/javascript" src="jquery-1.4.js"></script> </head> <body> <textarea style="300px; height:60px;" maxlength="10"></textarea> </body> </html> <script type="text/javascript"> $(function(){ $("textarea[maxlength]").keyup(function(){ var area=$(this); var max=parseInt(area.attr("maxlength"),10); //获取maxlength的值 if(max>0){ if(area.val().length>max){ //textarea的文本长度大于maxlength area.val(area.val().substr(0,max)); //截断textarea的文本重新赋值 } } }); }); </script>