web页面全角&半角

根据Unicode编码,全角空格为12288,半角空格为32 ;

其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 

  • 全角-->半角函数 
//半角转换为全角函数 
function ToBCD(str) { 
    var tmp = ""; 
    for(var i=0;i<str.length;i++) {
     var code = str.charCodeAt(i);//获取当前字符的Unicode编码
        if(code>=65281 && code<=65373) { //Unicode编码中所有的全角英文字符以及各种字符
            tmp += String.fromCharCode(str.charCodeAt(i)-65248); 
        } else if(code==12288) {//空格
            tmp += String.fromCharCode(str.charCodeAt(i)-12256);
        } else { 
            tmp += String.fromCharCode(str.charCodeAt(i)); 
        } 
    } 
    return tmp 
}                
lift is made up of small pleasures. 生活是由各种微小的幸福构成。 日积月累,就会产生意想不到的Miracles。 每一天的坚持,每一天的收获,我与你同在!!
原文地址:https://www.cnblogs.com/drubber/p/6006090.html