复制代码
- <!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">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"/>
- <script type="text/javascript">
- function checkLength(){
- var curLength = $("#test").value.length;
- //alert("已经输入的字数:" + curLength );
- if (curLength>150){
- alert("超过字数限制,多出的字将被截断!" );
- $("#test").value = $("#test").value.substr(0,150);
- curLength = 150;
- }
- $("#textCount").innerHTML = 150-$("#test").value.length;
- }
- $(function(){
- if(/msie/i.test(navigator.userAgent)){
- $("#test").onpropertychange = checkLength;
- } else {
- $("#test").addEventListener("input",checkLength,false);
- }
- });
- </script>
- </head>
- <body>
- <label>
- 剩下<span id="textCount">150</span><br />
- <textarea id="#test" cols="45" rows="5"></textarea>
- </label>
- </body>
- </html>
|