正在检查元素中的十进制字符代码



我正在为我的表制作一个排序功能,因此我使用了十进制字符代码。。。

▲
▼

以创建上下箭头的表示。当我点击箭头时,它会调用一个函数来进行排序(以及切换箭头的方向(。为了做到这一点,我一直在尝试使用以下代码。。。

const assn = this.id;
const text = document.getElementById(assn).innerHTML;
if (text === "▼") {
document.getElementById(assn).innerHTML = "▲";
}
else {
document.getElementById(assn).innerHTML = "▼";
}

然而,这似乎不起作用,因为变量文本似乎保存为文字箭头本身,并与文字字符串"进行比较&9660";而不是该字符串将创建的箭头。我想知道这是否是原因,如果是,有没有什么方法可以将字符代码进行比较?

我也尝试过使用document.getElementById(assn(.textContent,但这似乎有同样的效果。谢谢你的帮助。

为什么不试试CSS?下面最简单的版本:

<style type="text/css">
.arrowControl > span {display:none;}
.arrowControl.down > span.down
, .arrowControl.up > span.up {display:inline;}
</style>
<div id="anyContainer">
<div class="arrowControl">
<span class="down" onclick="funcForDown();">&#9660;</span>
<span class="up" onclick="funcForUp();">&#9650;</span>
</div>
</div>

然后在你的JS 中

var arrow=document.querySelector('#anyContainer .arrowControl');
if (arrow.classList.contains('up')){
arrow.classList.remove('up');
arrow.classList.add('down');
}
else {
arrow.classList.remove('down');
arrow.classList.add('up');
}

最新更新