zoukankan      html  css  js  c++  java
  • JS 判断字串字节数,并截取长度

    JS 判断字串字节数,并截取长度

     
    var matchWords;
            function notifyTextLength() {
                var inputNum = document.getElementById("txtTitle").value.replace(/[^x00-xff]/g, "**").length; //得到输入的字节数
                if (inputNum <= 200) {
                    matchWords = document.getElementById("txtTitle").value.length;
                    document.getElementById("inputedWord").innerHTML = inputNum + "字节," + matchWords + "字符";
                    document.getElementById("inputtingWord").innerHTML = (200 - inputNum) + "字母,"+(Math.round(((200-inputNum)/2)-0.5))+"汉字";
                }
                if (inputNum > 200) {
                    document.getElementById("txtTitle").value = document.getElementById("txtTitle").value.substring(0, matchWords);

     //如果超过200字节,就截取到200字节

                 }
                   
            }
    <input id="txtTitle" type="text" class="inputText" runat="server" onpropertychange="notifyTextLength();" />

    当前已经输入<span id="inputedWord" style="color:red"></span> 还可以输入<span id="inputtingWord" style="color:Red;"></span>

    本文转载自:http://www.cnblogs.com/redvito/p/6114826.html

  • 相关阅读:
    字符编码与函数
    linux打印彩色字
    企业级docker仓库Harbor部署
    PyPI使用国内源
    CentOS 7.2 升级内核支持 Docker overlay 网络模式
    购物车2
    购物车
    定制 cobbler TITLE 信息
    06.密码错误3次锁定
    05.for循环语句
  • 原文地址:https://www.cnblogs.com/Eton/p/6115345.html
Copyright © 2011-2022 走看看