gpt4 book ai didi

javascript - 如何用字符编码输出Javascript字符串

转载 作者:行者123 更新时间:2023-12-01 02:28:23 25 4
gpt4 key购买 nike

所以 - 我在 javascript 中有一个字符串,喜欢这样:

var foo = "Hello™";

我需要用 HTML(通过 JS)打印出来,所以它看起来像这样:

你好™

听起来很简单,但我显然错过了一些东西,因为我还无法找到一个简单的解决方案。

编辑 - 这是更多代码:

var label = document.createElement('label');
var foo = selectData.options[i][1]; // this is "Hello&trade";

label.innerHTML = foo;

container.appendChild( label );

最佳答案

只需将其附加到 innerHTML 属性中即可。innerHTML 会对给定的输入进行 html 编码,并将其附加到节点。

var foo = "Hello™";

document.getElementById('label').innerHTML = foo;
<label id="label"></label>

关于javascript - 如何用字符编码输出Javascript字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48528999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com