2013-07-17 116 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())