jquery .text() and unicode

后端 未结 4 948
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 15:03

I\'d like to display the \"Open Lock\" character in my HTML link text.

If I do it directly it shows up correctly with 🔒<

4条回答
  •  情深已故
    2021-02-01 15:35

    Following script should convert UTF32 hex values to UTF16 pairs

    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 {}
        }
    }
    
    
    

提交回复
热议问题