javascript,漢字轉unicode|unicode轉漢字

複製到一個html中就能使用,這裏小記錄一下。
<HTML>
<head>
<title>漢字轉unicode|unicode轉漢字</title>
<style type="text/css">
<!--
td {color: #FF9900}
A {
	COLOR: #66CC22; TEXT-DECORATION: none
}
A:hover {
	COLOR: #ff0000; TEXT-DECORATION: none
}
.gree {color: #66CC33}
.btn {font-family: Verdana, Arial, Helvetica, sans-serif; 
font-size: 12px; 
color: #666666; 
text-align: left; 
text-decoration: none; 
display: block; 
overflow: visible; 
margin-right: 10px; 
margin-left: 10px; 
}
-->
</style>
</head>
<body >
<table width="728" height="54" border="0" align="center" cellpadding="0" cellspacing="1" >
  <tr>
    <td height="52"><table width="726" height="46"  border="0" cellpadding="0" cellspacing="0">
      <tr align="center">
          <td>
            <TABLE style="MARGIN-TOP: 6px" cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TR>
                <TD width="774" vAlign=top style="PADDING-RIGHT: 6px; PADDING-LEFT: 6px"><h2 align="center"> unicode轉化漢字 漢字轉化unicode編碼</h2>

                  <hr size="1">
                  <CENTER>
<TEXTAREA name="source" cols="100" rows="14" id="source">\u7D2B\u964Cl\u7EA4\u5C18\u7684\u4E13\u680F\uFF0C\u6700\u7F8E\u4E0D\u8FC7\u5E73\u6DE1\uFF0C\u6700\u771F\u4E0D\u8FC7\u5E73\u51E1\u3002</TEXTAREA>
</CENTER>
                  <BR>
                  <CENTER>
                    <table width="400" border="0" cellspacing="2">
                      <tr>
                        <td><span class="btn"><a href="javascript:action('CONVERT_FMT1')"> <strong>轉化成爲:<br>
                        </strong>&#xxxx </a></span></td>
                        <td><span class="btn"><a href="javascript:action('CONVERT_FMT2')"> <strong>轉化成爲unicode字符:<br>
                        </strong> \uxxxx </a></span></td>
                        <td><span class="btn"><a href="javascript:action('RECONVERT')"> <strong>轉化爲漢字:<br>
                        </strong> ::WV </a></span></td>
                      </tr>
                    </table>
                    <p>
                    <div id="tt" style="display:none"></div>
                    <textarea name="show2" rows="14" id="show2" cols="100"></textarea>
                    </p>
					<P style="MARGIN-TOP: 0px"> </P>
                </CENTER></TD>
                <TD noWrap width=1></TD>
              </TR>
            </TABLE>
          </td>
      </tr>
    </table></td>
  </tr>
</table>
<br>
</BODY>
<script language="javascript" type="text/javascript"> 
	var oSource = document.getElementById("source"); 
	var oShow2 = document.getElementById("show2"); 
	var oTt = document.getElementById("tt"); 
	function action(pChoice){ 
	switch(pChoice){ 
		case "CONVERT_FMT1": 
		oShow2.value = ascii(oSource.value); 
		break; 
		case "CONVERT_FMT2": 
		oShow2.value = unicode(oSource.value); 
		break; 
		case "RECONVERT": 
		oShow2.value = reconvert(oSource.value); 
		break; 
		} 
	} 
	function ascii(str){ 
		return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\&#x$2;")}); 
	} 
	function unicode(str){ 
		return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")}); 
	} 
	function reconvert(str){ 
		str = str.replace(/(\\u)(\w{4})/gi,function($0){ 
		return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16))); 
	}); 
	str = str.replace(/(&#x)(\w{4});/gi,function($0){ 
	return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16)); 
	}); 
	return str; 
	} 
</script>
</HTML>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章