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

js 将汉字转化成unicode 16 编码 简单例子

2013年08月23日 ⁄ 综合 ⁄ 共 737字 ⁄ 字号 评论关闭

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
    </head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
     <input id="txt_edit"  value="测试" /><input  type="button" onclick="encode(this);"  value="测试" />
    </form>
</body>
<script>
  
    var mode="zhuan";
    function encode(btn)
    {
    var obj=document.getElementById("txt_edit");
    if(mode=="zhuan")
        {
             obj.value=obj.value.replace(/[^\u0000-\u00FF]/g,function($0)
             {
             return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")
             });
         btn.value="还原";
         mode="huan";
        
        }else
        {
         obj.value=unescape(obj.value.replace(/\\u/g,'%u'));
         btn.value="转化";
         mode="zhuan";
        }
    }
</script>
</html>

抱歉!评论已关闭.