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

时间:2021-05-08 01:05:48

标签: javascript html

我正在参考此代码片段来处理向下滚动的效果。

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

我想为按钮添加一个向下箭头。箭头的 unicode 是 Hex2193。我将插入方法搜索到 html 和 js 代码中,如下所示。但是我的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;
    }
}

1 个答案:

答案 0 :(得分:0)

你可以这样写

**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/

找到所有 unicode 符号