jquery .text() and unicode

后端 未结 4 950
没有蜡笔的小新
没有蜡笔的小新 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:48

    Javascript internally only supports 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:

    0xD83D 0xDD13
    

    i.e.

    $('#myId').text('\ud83d\udd13');
    

    More details can be found in RFC 4627, which is strictly speaking the format for JSON.

提交回复
热议问题