首页 >web前端 >js教程 >Javascript实现真实字符串剩余字数提示的实例代码_javascript技巧

Javascript实现真实字符串剩余字数提示的实例代码_javascript技巧

WBOY
WBOY原创
2016-05-16 17:19:23999浏览

复制代码 代码如下:

//文本框剩余字数提示(字符大小)
function textLimitCheckSj(thisArea, maxLength, SpanId) {
    var str = thisArea.value;
    if (getChrLen(str, maxLength) > maxLength * 2) {
        thisArea.value = str.substring(0, x-1);
    }
    else {
        var varss = '(剩余字数:' + Math.floor((maxLength * 2 - getChrLen(str, maxLength)) / 2) + ')';
        document.getElementById(SpanId).innerHTML = varss;
    }
}
function getChrLen(str, maxLength) {
    var realLength = 0, len = str.length, charCode = -1;
    x = 0;
    for (; (x         charCode = str.charCodeAt(x);
        if (charCode >= 0 && charCode             realLength += 1;
        else
            realLength += 2;
    }
    return realLength;
}
?
                        onkeyup="textLimitCheckSj(this,50,'spOwner_Name')">                            style="color: #808080">(50字以内)

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn