2013-07-17 63 views
1

我有一个简单的脚本,当标题匹配时,它正在寻找一个标题来执行一个函数。我遇到了版权和注册商标等特殊字符的问题。引发问题的jQuery特殊字符

var isDisclosure = $('.disclosure-container h3').html(); 

if (isDisclosure == "Awesome Product<sup>&#174;</sup> Disclosures") { 
     alert('zomg'); 
    } 

的HTML看起来像这样的萤火:

<h3 class="inline"> 
    Awesome Product 
    <sup>®</sup> 
    Disclosures 
</h3> 

在查看HTML看起来像这样的源...

<h3 class="inline">Awesome Product<sup>&#174;</sup> Disclosures</h3> 

所以,当我是HTML添加到,如果声明,我得到一个额外的字符,它不工作...像这样...

if (isDisclosure == "Awesome Product<sup>©</sup> Disclosures") 

我很开放,只是用通配符或最后的东西搜索“Awesome Product”。

+0

那么,在这种情况下,你可以做'如果(isDisclosure.indexOf( “真棒产品”)=== 0){...':) – techfoobar

+0

您是否尝试过使用它转换HTML实体的功能等一个[这里](http://stackoverflow.com/questions/1912501/unescape-html-entities-in-javascript)并用它来填充你的字符串。换句话说'if(isDisclosure ==“Awesome Product ”+ htmlDecode(“&#174”)+“ Disclosures”)' – Jnatalzia

回答

1

你可以使用正则表达式测试部分匹配

if(/Awesome\sProduct<sup>.+</sup>\sDisclosures/i.test(isDisclosure)) 

if(isDisclosure.indexOf("Awesome Product") >= 0) 

索引或者你可以从一个隐藏的div引用的HTML。

<div id="refs" style="display:none;"> 
<h3 id="awesome"> 
    Awesome Product 
    <sup>®</sup> 
    Disclosures 
</h3> 
</div> 

if(isDisclosure == $("#refs #awesome").html())