2014-07-25 72 views
2

我昨天(Hide element if it contains specific text)問過類似的問題隱藏DIV,如果它包含特定文本

我曾經試圖去適應由dystroy建議隱藏另一頁上一個DIV的代碼。

這是HTML頁面:

<div class="ct_pl_product_price"> 
<a class="ct_pl_product_link" href="/link/to/item">£0.00</a> 
</div> 

和以前一樣,我想隱藏 「ct_pl_product_price」 如果DIV包含£0.00

這是我已經嘗試使用該腳本:

$(document).ready(function(){ 

$(".ct_pl_product_price").filter(function(){ 
    return $(this).clone().find('.ct_pl_product_link').remove().end().text().trim()=="0.00" 
}).hide() 

}); 

我認爲這是有道理的(如果我已經正確解釋dystroys的代碼),但它似乎沒有工作。

任何想法?

回答

9

您可以在此情況下,使用:contains選擇:

$('.ct_pl_product_price:contains("£0.00")').hide() 
+0

謝謝,這非常有幫助。出於興趣,我不認爲你可以解釋爲什麼其他代碼不起作用? – user3220812

+0

@ user3220812對於一個開始,你錯過了「£」在你的公式 – Banana

+1

@ user3220812:因爲,find('。ct_pl_product_link')。remove()'刪除其中的文本。 –

2

您可以使用:contains選擇:

$('.ct_pl_product_price:contains("0.00")').hide(); 

或者你可以使用filter,修改邏輯:

$(".ct_pl_product_price").filter(function(){ 
    return $(this).text().indexOf('0.00') != -1; 
}).hide() 

請注意filter()會稍快。

相關問題