zoukankan      html  css  js  c++  java
  • 仿发送微博限制只发送140个字(一个汉字占位2字节)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
        $("textarea").keyup();
        });
        
        function strLenCalc(obj, checklen, maxlen) {
         var v = obj.val(), charlen = 0, maxlen = !maxlen ? 200 : maxlen, curlen = maxlen, len = v.length;
         for(var i = 0; i < v.length; i++) {
         if(v.charCodeAt(i) < 0 || v.charCodeAt(i) > 255) {
                curlen -= 1;
        }
        }
      if(curlen >= len) {
            $("#"+checklen).html("还可输入 <strong>"+Math.floor((curlen-len)/2)+"</strong> 个字").css('color', '');
            $("#subBtn").removeAttr("disabled");
            } else {
            $("#"+checklen).html("已经超过 <strong>"+Math.ceil((len-curlen)/2)+"</strong> 个字").css('color', '#FF0000');
            $("#subBtn").attr("disabled", "disabled");
            }
        }
    </script>
    <body>
    <div>
    <form action="index.php" >
    <textarea tabindex="11" onkeyup="strLenCalc($(this), 'checklen', 280);" value="" class="px" name="subject" id="subject"></textarea>
    <span id="checklen">还可输入 <strong>140</strong> 个汉字</span>
    <input type="submit" id="subBtn" value="发布" />
    </form>
    </div>
    </body>
    </html>

  • 相关阅读:
    tectangular container
    WIFI
    微信小程序动态改变数组或对象中的某个属性值
    常用的正则表达式
    前端登录通过账号显示对应头像
    JS返回页面时自动回滚到历史浏览位置
    JavaScript让登录或搜索文本框自动获得焦点
    react脚手架应用
    npm教程3_脚手架原理以及bootstrap引入
    npm教程2
  • 原文地址:https://www.cnblogs.com/riskyer/p/3358232.html
Copyright © 2011-2022 走看看