test.htm是uft-8格式的,但文件里面没写
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
这样用浏览器打开文件,看到的是乱码的,需要通过“查看-字符编码-utf-8”来查看正常效果。请问能否通过js实现以上操作?
下面的代码经测试无效:
<head>
<script type="text/javascript">
document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function set_utf(){
$("head").append(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
);
}
</script>
</head><body>
<div>我是utf-8</div>
<button onclick="set_utf()">set_uft</button>
</body>