如何将html实体与jQuery进行比较 [英] How to compare an html entity with jQuery

查看:91
本文介绍了如何将html实体与jQuery进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML代码:

I have the following html code:

<h3 id="headerid"><span onclick="expandCollapse('headerid')">&uArr;</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 == "&dArr;") {     
        $("#"+id+" span").html("&uArr;");               
    } else {        
        $("#"+id+" span").html("&dArr;");               
    }
}

我的功能始终是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆