<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>汉字与unicode码互相转换</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function changeinput1(){
var str=document.getElementById("input1").value;
var a = [], i = 0;
for (; i < str.length ;) a[i] = str.charCodeAt(i ++);
var sss= "&#" + a.join(";&#") + ";";
document.getElementById("output1").value=sss;
}
function changeinput2(){
var str=document.getElementById("input2").value;
var code = str.match(/&#(\d+);/g);
if (code == null) {
document.getElementById("output2").value='你输入的unicode码不标准!';
return '';
}
var result = '';
for (var i=0; i<code.length; i++)
result += String.fromCharCode(code[i].replace(/[&#;]/g, ''));
document.getElementById("output2").value=result;
}
</script>
</head>
<body>
<table align="center" border="1">
<tr>
<td>
汉字转unicode编码:
</td>
<td>
<input id="input1" onChange="changeinput1()">
</td>
<td>
<input id="output1" onkeydown="return false">
</td>
</tr>
<tr>
<td>
unicode编码转汉字:
</td>
<td>
<input id="input2" onChange="changeinput2()" style="ime-mode:disabled">
</td>
<td>
<input id="output2" onkeydown="return false">
</td>
</tr>
</table>
</body>
</html>
晕,上面的代码是有问题的,直接打开的时候IE直接显示空白,但是查看源文件显示它已经全部加载完成了,后来在网上找到原因了,原来页面编码utf8是在meta中定义的,浏览器在读到编码前,认不出中文的title,于是解析失败!所以meta中的content-type必须要放在最上面,一下是修改后的代码: