UNI code点在浏览器中呈现的区别:HTML字符VS jQuery.html [英] Unicode points rendering difference in a browser: HTML characters vs jQuery.html
问题描述
让我们说我有以下几个基本的HTML页面
Let's say I have the following basic HTML page
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
\u00f2
</body>
</html>
在页面呈现,我所看到的是\\ u00f2,而我期待O操作。还有来自大,而是。随着下面的JavaScript code,我看到的是ò字符(2秒后)。
When the page renders, what I see is \u00f2 whereas I was expecting ò. And there comes the big "but". With the following Javascript code, what I see is the ò character (2 seconds later).
$(function(){
window.setTimeout(function(){
$("body").html("\u00f2")},2000);
});
});
我的问题是,为什么会出现这种情况?我所知道的,而不是渲染的Uni code codepoints,我可以将它们转换为HTML实体,并直接呈现正确的字符。现在的问题是更多的学习目的。
My question is, why is this happening? I am aware of rather than rendering the Unicode codepoints, I could convert them to HTML entities and render the correct character directly. The question is more for learning purposes.
下面是的jsbin
推荐答案
这是因为在HTML中, \\ u00f2
只是一个五个字符序列;反斜线 \\
从来没有在HTML中的任何特殊含义。在JavaScript字符串,\\ u00f2有着特殊的意义:它表示的Uni code code单元十六进制数00F2,即chacterO
It happens because in HTML, \u00f2
is just a sequence of five characters; the backslash \
never has any special meaning in HTML. In JavaScript strings, \u00f2 has a special meaning: it denotes the Unicode code unit with hexadecimal number 00f2, i.e. the chacter "ò".
相反,虽然可以使用&放大器;#x00f2;
在HTML中表示O,你不能这样做,在JavaScript中,虽然你可以使用转换函数&放大器;#x00f2;
(这仅仅是一个从视图中的JavaScript点八个字符序列)到O。此外,如果嵌入在HTML中的剧本
元素或事件属性,那么浏览器可能会根据一定的规则,第一间$ P出现在您的JavaScript code $ PT &放大器;#x00f2;
通过HTML规则调用JavaScript的跨preTER之前
Conversely, although you can use ò
in HTML to denote "ò", you cannot do that in JavaScript, though you could use functions that convert ò
(which is just a sequence of eight characters from the JavaScript point of view) to "ò". Moreover, if your JavaScript code appears as embedded in HTML in a script
element or in an event attribute, then browsers may, depending on certain rules, first interpret ò
by HTML rules before invoking the JavaScript interpreter.
在HTML文档,现代,一般推荐的方法是直接输入字符,使用UTF-8编码。你可以做同样的JavaScript中,也是如此,例如 $(身体)HTML(O)},2000)
。然而,这有时避免由于假定或真实并发症指定字符编码一>
In HTML documents, the modern, generally recommendable method is to enter the characters directly, using the UTF-8 encoding. You can do the same in JavaScript, too, e.g. $("body").html("ò")},2000)
. However, this is sometimes avoided due to assumed or real complications in specifying the character encoding.
这篇关于UNI code点在浏览器中呈现的区别:HTML字符VS jQuery.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!