0
点赞
收藏
分享

微信扫一扫

js 平时经常用的

/** 得到字符串的字符长度(一个汉字占两个字符长)*/

    function getBytesLength(str) {

        // 在GBK编码里,除了ASCII字符,其它都占两个字符宽

        return str.replace(/[^x00-xff]/g, 'xx').length;

    }


    /** * 根据字符长来截取字符串  */

    function subStringByBytes(val, maxBytesLen) {

        var len = maxBytesLen;

        var result = val.slice(0, len);

        while(getBytesLength(result) > maxBytesLen) {

            result = result.slice(0, --len);

        }

        return result;

    }




 function GetLength (str) {    

    ///<summary>获得字符串实际长度,中文2,英文1</summary>    

    ///<param name="str">要获得长度的字符串</param>    

    var realLength = 0, len = str.length, charCode = -1;    

    for (var i = 0; i < len; i++) {    

        charCode = str.charCodeAt(i);    

        if (charCode >= 0 && charCode <= 128) realLength += 1;    

        else realLength += 2;    

    }    

    return realLength;    

}



ListBox移除多選項目

移除項目的JS,大部分會忽略掉Remove後項目index的變動,造成無法真正移除多選項目,因此將修正後的Code貼在這裡。

function RemoveListBoxSelected(ListBoxID) {

    var ListBox = document.getElementById(ListBoxID); 

    for (i = 0; i < ListBox.options.length; i++) {

        if (ListBox.options[i].selected) {

            ListBox.remove(i);

            i--;

        }

    }

}


举报

相关推荐

0 条评论