简体   繁体   English

如何动态设置 emoji unicode

[英]How to set emoji unicode dynamically

This is what my html code looks like:这是我的 html 代码的样子:

<html>
<style>
    body {
        font-size: 18px;
    }
</style>
<body>
    <span id="tiger" style='font-size:100px;'>&#128161;</span>
    <button id="btn">SET TIGER</button>

    <script>
        document.getElementById('btn').addEventListener('click', function (e) {
            document.getElementById('tiger').innerText = `&#129409;`;
        });
    </script>
</body>
</html>

But it doesn't work and shows just &#129409;但它不起作用,只显示&#129409; instead of emoji when click button.单击按钮时而不是表情符号。 Why is that, how can I set emoji dynamically?为什么会这样,我如何动态设置表情符号?

 <html> <style> body { font-size: 18px; } </style> <body> <span id="tiger" style='font-size:100px;'>&#128161;</span> <button id="btn">SET TIGER</button> <script> var code = '129409'; // document.getElementById('btn').addEventListener('click', function (e) { document.getElementById('tiger').innerText = String.fromCodePoint(parseInt(code));; }); </script> </body> </html>

Kindly check below, Hope your query is resolved.请检查以下,希望您的查询得到解决。

<html>
<style>
    body {
        font-size: 18px;
    }
</style>
<body>
    <span id="tiger" style='font-size:100px;'>&#128161;</span>
    <button id="btn">SET TIGER</button>

    <script>
    var code = '129409';
         //
        document.getElementById('btn').addEventListener('click', function (e) {
            document.getElementById('tiger').innerText = String.fromCodePoint(parseInt(code));;
        });
    </script>
</body>
</html>

HTML escapes like "&#128161;" HTML 转义,如“&#128161;” are decoded by the HTML parser when the source document is being parsed.在解析源文档时由 HTML 解析器解码。

To set the content of a text node, use a JavaScript string - which technically is encoded in UTF-16.要设置文本节点的内容,请使用 JavaScript 字符串——技术上以 UTF-16 编码。

If you knew the hexadecimal representation of the character in UTF-16 you could set text node content using an escaped Javascript like如果您知道 UTF-16 中字符的十六进制表示,则可以使用转义的 Javascript 设置文本节点内容,例如

element.textContent = "\ud83d\udca1"; // not recommended.

If you knew the Unicode value in hex or decimal you could use the static String fromCodePoint method:如果您知道十六进制或十进制的 Unicode 值,您可以使用静态 String fromCodePoint方法:

 element.innerText = String.fromCodePoint( 0x1f4a1) // or
 element.innerText = String.fromCodePoint( 128161 )

However, the method I would recommend is to encode the HTML file in UTF-8 and set the text content with a standard string value:但是,我推荐的方法是以 UTF-8 对 HTML 文件进行编码,并使用标准字符串值设置文本内容:

 element.innerText = '💡'

Limited code editor support for Unicode fonts makes this a bit difficult, of course.当然,对 Unicode 字体的有限代码编辑器支持使这有点困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM