简体   繁体   English

如何将包含十六进制 unicode 字符的 innerHTML 更改为另一个与 JavaScript 类似的字符?

[英]How do I change innerHTML which contains hex unicode character for another similar character with JavaScript?

I have a website menu that shows the down-pointing arrow next to an item that has a submenu.我有一个网站菜单,它在具有子菜单的项目旁边显示向下箭头。 This arrow ( ▼ ) is contained in a span like this: <span class="h-open-icon>&#9660;</span> and I want to change it to this: <span class="h-open-icon>&#9650;</span> (this character: ▲)此箭头 ( ▼ ) 包含在这样的跨度中: <span class="h-open-icon>&#9660;</span>我想将其更改为: <span class="h-open-icon>&#9650;</span> (这个字符:▲)

In Javascript, I am attempting to use the following code (part of a larger script) - which works fine for changing plain alpha-numeric text (eg innerHTML === "text" etc) - to change it to the up-pointing arrow when the submenu is open:在 Javascript 中,我正在尝试使用以下代码(较大脚本的一部分) - 它适用于更改纯字母数字文本(例如innerHTML === "text"等) - 将其更改为向上箭头当子菜单打开时:

let hstr = button.querySelector('.h-open-icon');
    if (hstr.innerHTML === "\&\#9660\;") {
        hstr.innerHTML = "\&\#9650\;";
    } else {
        hstr.innerHTML = "\&\#9660\;";
    }

But this is not working.但这不起作用。 I assume it must be something to do with escaping characters, but all I can find is help on how to remove them, not keep them.我认为这一定与 escaping 字符有关,但我能找到的只是如何删除它们的帮助,而不是保留它们。 It's just the numbers following the # that I need to change.我需要更改的只是 # 后面的数字。 Thanks.谢谢。

You could sidestep the issue by just including the symbols in the JS:您可以通过在 JS 中包含符号来回避这个问题:

 let hstr = document.getElementById("toggle"); hstr.onclick = function(){ if (hstr.innerHTML == "▼") { hstr.innerHTML = "▲"; } else { hstr.innerHTML = "▼"; } }
 <button id=toggle>▼</button>

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

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