如何使用 JavaScript/jQuery 从 HTML 中获取符号的 unicode/hex 表示形式?

发布于 2024-11-19 19:04:44 字数 562 浏览 2 评论 0原文

假设我有一个像这样的元素...

<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mo class="symbol">α</mo>
</math>

有没有办法使用 JavaScript/jQuery 获取 alpha α&#x03B1 的 unicode/hex 值?就像...

$('.symbol').text().unicode(); // I know unicode() doesn't exist
$('.symbol').text().hex(); // I know hex() doesn't exist

我需要 &#x03B1 而不是 α ,似乎任何时候我将 &#x03B1 插入 DOM 并尝试立即检索它,它被渲染,我无法取回 &#x03B1 ;我刚得到α。

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...

$('.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 α.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

未蓝澄海的烟 2024-11-26 19:04:44

使用大部分纯 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 + ";";
}

这是一个示例: http://jsfiddle.net/btWur/

Using mostly plain JavaScript, you should be able to do:

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/

忆梦 2024-11-26 19:04:44

charCodeAt 将获取字符串的十进制值:

"α".charCodeAt(0); //returns 945
0x03b1 === 945; //returns true

toString 将获取十六进制字符串

(945).toString(16); // returns "3b1"

(已确认在 IE9 和 Chrome 中工作)

charCodeAt will get you the decimal value of the string:

"α".charCodeAt(0); //returns 945
0x03b1 === 945; //returns true

toString will then get the hex string

(945).toString(16); // returns "3b1"

(Confirmed to work in IE9 and Chrome)

就此别过 2024-11-26 19:04:44

如果您尝试将 Unicode 字符从 BMP(基本多语言平面) 转换出来以上述方式 - 你会遇到令人不快的惊喜。 BMP 之外的字符被编码为多个 UTF16 值,例如:

"

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:

"????".length = 2 (one part for shackle one part for lock base :) )

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.

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.

Hope this saves you some time.

舞袖。长 2024-11-26 19:04:44

例如,如果您需要将此十六进制代码转换为 unicode

e68891e4bda0e4bb96

  1. 一次选择两个字符,
  2. 如果 dec ascii 代码超过 127,请在
  3. 返回 url 解码字符串

    之前添加 %

    函数 hex2a(hex) {
    var str = '';
    for (var i = 0; i < hex.length; i += 2){

     var dec = parseInt(hex.substr(i, 2), 16);
        字符 = String.fromCharCode(dec);
    
    
        如果 (12 月 > 127)
            字符 = "%"+hex.substr(i,2);
    
        str += 字符;
    
    }
    
    返回解码URI(str);
    

    }

for example in case you need to convert this hex code to unicode

e68891e4bda0e4bb96

  1. pick two character time by time ,
  2. if the dec ascii code is over 127 , add a % before
  3. 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);
    

    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文