如何将html实体与jQuery进行比较 [英] How to compare an html entity with jQuery
问题描述
我有以下HTML代码:
I have the following html code:
<h3 id="headerid"><span onclick="expandCollapse('headerid')">⇑</span>Header title</h3>
每当用户点击span标记时,我想在向上箭头和向下箭头之间切换。
I would like to toggle between up arrow and down arrow each time the user clicks the span tag.
function expandCollapse(id) {
var arrow = $("#"+id+" span").html(); // I have tried with .text() too
if(arrow == "⇓") {
$("#"+id+" span").html("⇑");
} else {
$("#"+id+" span").html("⇓");
}
}
我的功能始终是else路径。如果我做了一个javacript:警告 arrow
变量我得到的html实体表示为箭头。如何告诉jQuery将箭头
变量解释为字符串而不是html。
My function is going always the else path. If I make a javacript:alert of arrow
variable I am getting the html entity represented as an arrow. How can I tell jQuery to interpret the arrow
variable as a string and not as html.
推荐答案
解析HTML时,JQuery在DOM中看到的是 UPWARDS DOUBLE ARROW
(⇑),而不是实体参考。因此,在您的Javascript代码中,您应该测试⇑
或\ u21d1
。此外,您需要更改要转换的内容:
When the HTML is parsed, what JQuery sees in the DOM is a UPWARDS DOUBLE ARROW
("⇑"), not the entity reference. Thus, in your Javascript code you should test for "⇑"
or "\u21d1"
. Also, you need to change what you're switching to:
function expandCollapse(id) {
var arrow = $("#"+id+" span").html();
if(arrow == "\u21d1") {
$("#"+id+" span").html("\u21d3");
} else {
$("#"+id+" span").html("\u21d1");
}
}
这篇关于如何将html实体与jQuery进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!