Javascript jquery .text() 和 unicode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11087027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery .text() and unicode
提问by janesconference
I'd like to display the "Open Lock" characterin my HTML link text.
我想在我的 HTML 链接文本中显示“打开锁定”字符。
If I do it directly it shows up correctly with <a id="myId">🔒</a>
, but I found no way to change it dinamically with the jQuery .text()
function, like in:
如果我直接这样做,它会用 正确显示<a id="myId">🔒</a>
,但我发现无法使用 jQuery.text()
函数动态更改它,例如:
$("#myID").text(openLockText);
What should I put in openLockText?
我应该在 openLockText 中放入什么?
回答by Alnitak
Javascript internally only supports UTF-16.
Javascript 内部仅支持 UTF-16。
Because this is an extended 32-bit UTF character (not in the "Basic Multilingual Plane") you need to insert the "UTF-16 surrogate pair", which is helpfully provided on the same page that you linked to:
因为这是一个扩展的 32 位 UTF 字符(不在“基本多语言平面”中),您需要插入“UTF-16 代理对”,它在您链接到的同一页面上很有帮助:
0xD83D 0xDD13
i.e.
IE
$('#myId').text('\ud83d\udd13');
More details can be found in RFC 4627, which is strictly speaking the format for JSON.
更多细节可以在RFC 4627 中找到,严格来说这是 JSON 的格式。
回答by Pointy
edited— Ifit were a Unicode code point that could be represented in a single UTF-16 character, then ou could use JavaScript escape sequences in such situations:
已编辑—如果它是可以用单个 UTF-16 字符表示的 Unicode 代码点,那么您可以在这种情况下使用 JavaScript 转义序列:
$('#foo').text('\uXXXX');
However, because your character requires more bits, that doesn't work. It would probably be possible to construct the byte sequence that'd allow the character to be represented as UTF-16, but it'd be a pain. I'd go with .html()
.
但是,因为您的角色需要更多位,所以这是行不通的。可能可以构造允许字符表示为 UTF-16 的字节序列,但这会很痛苦。我会去的.html()
。
Note that not all fonts provide glyphs for "exotic" code points like that, and in my experience those that do provide incredibly ugly ones.
请注意,并非所有字体都为这样的“异国情调”代码点提供字形,根据我的经验,那些确实提供了非常丑陋的字形。
回答by Jukka K. Korpela
You can put the character there directly, as a quoted string, e.g.
您可以将字符直接放在那里,作为带引号的字符串,例如
$("#myID").text('');
provided that the file is UTF-8 encoded and you properly declare the character encoding. (In theory, you could alternatively use UTF-16 or even UTF-32, but browsers should not be expected to support them.)
前提是文件是 UTF-8 编码的,并且您正确声明了字符编码。(理论上,您也可以使用 UTF-16 甚至 UTF-32,但不应期望浏览器支持它们。)
Although support to non-BMP characters directly in source documents is optional according to the ECMAScript standard, modern browsers let you use them. Naturally, you need an editor that can handle UTF-8, and you need some input method(s); see e.g. my Full Unicode Inpututility.
尽管根据 ECMAScript 标准直接在源文档中支持非 BMP 字符是可选的,但现代浏览器允许您使用它们。自然,您需要一个可以处理 UTF-8 的编辑器,并且您需要一些输入法;参见例如我的完整 Unicode 输入实用程序。
The question contains some mistakes that have gone unnoticed: Since id
attribute values are case-sensitive, the spelling myId
needs to be fixed to myID
. And the OPEN LOCK character is U+1F513, not U+1F512, so the reference 🔒
would give a wrong character.
该问题包含一些未被注意的错误:由于id
属性值区分大小写,因此myId
需要将拼写固定为myID
。并且 OPEN LOCK 字符是 U+1F513,而不是 U+1F512,因此引用🔒
会给出错误的字符。
Moreover, very few fonts contain OPEN LOCK, and browsers, especially IE, may have difficulties in finding the glyph even if some font in the system contains it, so you should give browsers help and declare a list of fonts known to contain the character, in order of preference. Example:
此外,很少有字体包含OPEN LOCK,并且浏览器,尤其是IE,即使系统中的某些字体包含它也可能难以找到字形,因此您应该向浏览器提供帮助并声明已知包含该字符的字体列表,按优先顺序。例子:
<style>
#myID { font-family: Symbola, Quivira, Segoe UI Symbol; }
</style>
<a id="myID">stuff</a>
<script>
$("#myID").text('');
</script>
A non-BMP character is internally represented as a surrogate pair, and it couldbe written using \u
notations for the components of the pair, but this is very unintuitive
非 BMP 字符在内部表示为代理对,并且可以使用\u
该对的组件的符号来编写,但这非常不直观
回答by Matas Vaitkevicius
Following script should convert UTF32 hex values to UTF16 pairs
以下脚本应将 UTF32 十六进制值转换为 UTF16 对
function toUTF16Pair(hex) {
hex = hex.replace(/[&#x;]/g,'');
var x = parseInt(hex, 16);
if (x >= 0x10000 && x <= 0x10FFFF) {
var first = Math.floor((x - 0x10000) / 0x400) + 0xD800;
var second = ((x - 0x10000) % 0x400) + 0xDC00;
return {
first: first.toString(16).toUpperCase(),
second: second.toString(16).toUpperCase(),
combined: '\u'+first.toString(16).toUpperCase() + '\u'+second.toString(16).toUpperCase()
};
} else {
return {}
}
}
<input type='text' id='in' />
<input type='button' value='Click' onclick="document.getElementById('result').innerHTML = toUTF16Pair(document.getElementById('in').value).combined" />
<p id='result'></p>