为什么我的 js 代码无法识别 html unicode?



我正在通过引用这个代码片段来实现向下滚动效果。

https://codepen.io/1csyu/pen/eYvmKWJ?editors=1010

我想在按钮上加一个向下箭头。箭头的unicode是Hex2193。我在html和js代码中搜索了insert方法,如下所示。但是我的js代码无法识别它。有人能告诉我我做错了什么吗?非常感谢。

**html**
<div class="scroll-btn-container">
<button class="scroll-btn">Show More &#x2193;</button>
</div>
**js**
var downA = 'u2193';

function toggleDescriptionHeight(e) {
ele.classList.toggle('expanded');
if (e.target.innerHTML === 'Show More' + downA) {
e.target.innerHTML = 'Show Less' + downA;
} else {
e.target.innerHTML = 'Show More' + downA;
}
}

你可以这样写

**html**
<div class="scroll-btn-container">
<button class="scroll-btn">Show More &#x2193;</button>
</div>
**js**
function toggleDescriptionHeight(e) {
ele.classList.toggle('expanded');
if (e.target.innerHTML.includes('Show More')) {
e.target.innerHTML = 'Show Less ▲';
} else {
e.target.innerHTML = 'Show More ▼';
}
}

您可以将符号复制并粘贴到JS字符串中,也可以将符号直接存储在变量中。不需要unicode。您可以直接在https://unicode-table.com/en/

最新更新