现在的位置: 首页 > 综合 > 正文

我自己写的js 全角半角互相转化函数

2013年08月10日 ⁄ 综合 ⁄ 共 587字 ⁄ 字号 评论关闭

 

function tobanjiao(str) { 
		var result="";
		for (var i = 0; i < str.length; i++)
		{
			if (str.charCodeAt(i)==12288)
			{
				result+= String.fromCharCode(str.charCodeAt(i)-12256);
				continue;
			}
			if (str.charCodeAt(i)>65280 && str.charCodeAt(i)<65375)
			result+= String.fromCharCode(str.charCodeAt(i)-65248);
			else result+= String.fromCharCode(str.charCodeAt(i));
		} 
  
        return result; 
    }   
        
    function toquanjiao(mystr) {    
        var result="";
        for (var i=0; i < mystr.length ;i++) 
        {
		   if(mystr.charCodeAt(i)==32)
		   {
				result+= String.fromCharCode(str.charCodeAt(i)+12256);
				continue;
		   }
		  	
		   if ( mystr.charCodeAt(i)<127 )
		  	    result+= String.fromCharCode(mystr.charCodeAt(i)+65248);	   

	 }  

        return result;  
    }

抱歉!评论已关闭.