zoukankan      html  css  js  c++  java
  • js:输入字数限制

    Demo
    <!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>
    </head>
    <body>
    <textarea id="b" rows="4" cols="20" ></textarea>
    <div><span id="a">已输入字符: </span><input type="button" value="OK" onclick="cha()"></div>
    <script language="javascript">
    var maxl=3//总长
    document.onkeydown=function(){
       var s=document.getElementById("b").value.length +1;
       if(s>maxl)document.getElementById("b").value=document.getElementById("b").value.substr(0,maxl-1)
       else document.getElementById("a").innerHTML="已输入:"+s+"/"+maxl+" 字符"
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    0802作业1替换文本文件内容

    看病
    爬山
    作业1
    超市(未完成)
    图片复制
    替换
    文件
    英文字母和中文汉字在不同字符集编码下的字节数
  • 原文地址:https://www.cnblogs.com/tinyphp/p/3385633.html
Copyright © 2011-2022 走看看