如何使用 JavaScript/jQuery 从 HTML 中获取符号的 unicode/hex 表示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6639770/
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
How do I get the unicode/hex representation of a symbol out of the HTML using JavaScript/jQuery?
提问by Hristo
Say I have an element like this...
说我有一个这样的元素......
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mo class="symbol">α</mo>
</math>
Is there a way to get the unicode/hex value of alpha α
, α
, using JavaScript/jQuery? Something like...
有没有办法使用 JavaScript/jQuery获取 alpha α
,的 unicode/hex 值α
?就像是...
$('.symbol').text().unicode(); // I know unicode() doesn't exist
$('.symbol').text().hex(); // I know hex() doesn't exist
I need α
instead of α
and it seems like anytime I insert α
into the DOM and try to retrieve it right away, it gets rendered and I can't get α
back; I just get α.
我需要α
而不是,α
而且似乎每当我插入α
DOM 并尝试立即检索它时,它都会被渲染而我无法α
返回;我只得到α。
回答by aroth
Using mostly plain JavaScript, you should be able to do:
主要使用纯 JavaScript,您应该能够做到:
function entityForSymbolInContainer(selector) {
var code = $(selector).text().charCodeAt(0);
var codeHex = code.toString(16).toUpperCase();
while (codeHex.length < 4) {
codeHex = "0" + codeHex;
}
return "&#x" + codeHex + ";";
}
Here's an example: http://jsfiddle.net/btWur/
这是一个例子:http: //jsfiddle.net/btWur/
回答by Jim Deville
charCodeAt
will get you the decimal value of the string:
charCodeAt
将为您提供字符串的十进制值:
"α".charCodeAt(0); //returns 945
0x03b1 === 945; //returns true
toString
will then get the hex string
toString
然后将获得十六进制字符串
(945).toString(16); // returns "3b1"
(Confirmed to work in IE9 and Chrome)
(已确认在 IE9 和 Chrome 中工作)
回答by Matas Vaitkevicius
If you would try to convert Unicode character out of BMP (basic multilingual plane)in ways above - you are up for a nasty surprise. Characters out of BMP are encoded as multiple UTF16
values for example:
如果您尝试以上述方式将 Unicode 字符从BMP(基本多语言平面)中转换出来,那么您会遇到令人讨厌的惊喜。BMP 之外的字符被编码为多个UTF16
值,例如:
"".length
= 2 (one part for shackle one part for lock base :) )
"".length
= 2(一部分用于卸扣,一部分用于锁座:))
so "".charCodeAt(0)
will give you 55357
which is only 'half' of number while "".charCodeAt(1)
will give you 56594
which is the other half.
所以"".charCodeAt(0)
会给你55357
只有“一半”的数字,而"".charCodeAt(1)
会给你56594
另一半。
To get char codes for those values you might wanna use use following string extension function
要获取这些值的字符代码,您可能需要使用以下字符串扩展函数
String.prototype.charCodeUTF32 = function(){
return ((((this.charCodeAt(0)-0xD800)*0x400) + (this.charCodeAt(1)-0xDC00) + 0x10000));
};
you can also use it like this
你也可以这样使用
"&#x"+("".charCodeUTF32()).toString(16)+";"
to get html hex codes.
获取 html 十六进制代码。
Hope this saves you some time.
希望这可以为您节省一些时间。
回答by chings228
for example in case you need to convert this hex code to unicode
例如,如果您需要将此十六进制代码转换为 unicode
e68891e4bda0e4bb96
e68891e4bda0e4bb96
- pick two character time by time ,
- if the dec ascii code is over 127 , add a % before
return url decode string
function hex2a(hex) { var str = ''; for (var i = 0; i < hex.length; i += 2){
var dec = parseInt(hex.substr(i, 2), 16); character = String.fromCharCode(dec); if (dec > 127) character = "%"+hex.substr(i,2); str += character; } return decodeURI(str);
}
- 一次次选择两个字符,
- 如果 dec ascii 代码超过 127 ,则在前面添加 %
返回 url 解码字符串
函数 hex2a(hex) { var str = ''; for (var i = 0; i < hex.length; i += 2){
var dec = parseInt(hex.substr(i, 2), 16); character = String.fromCharCode(dec); if (dec > 127) character = "%"+hex.substr(i,2); str += character; } return decodeURI(str);
}