星空网 > 软件开发 > Java

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" runat="server" onpropertychange="notifyTextLength();" />

当前已经输入<span id="inputedWord" ></span> 还可以输入<span id="inputtingWord" ></span>

 




原标题:JS 判断字串字节数,并截取长度

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊跟卖是什么?亚马逊卖家该怎么防止被跟卖?:https://www.ikjzd.com/articles/19198
一周要闻!亚马逊整顿VC事件只为打假?:https://www.ikjzd.com/articles/19199
国外坛友对2018年亚马逊的几点大胆预测:https://www.ikjzd.com/articles/192
最全面的跨境电商平台选择建议:https://www.ikjzd.com/articles/19200
(精品分析)亚马逊法国站雨伞类目市场调查数据报告:https://www.ikjzd.com/articles/19207
亚马逊推出ReTargeting广告,你会用吗?:https://www.ikjzd.com/articles/19208
字节跳动辟谣!TikTok收紧美国开店政策为不实信息:https://www.goluckyvip.com/news/188212.html
2024北京庞各庄镇梨花节开幕时间是几号?:https://www.vstour.cn/a/365179.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流