簡體   English   中英

如果DIV包含特定文本,則隱藏DIV

[英]Hiding DIV if it contains specific text

昨天我問了一個類似的問題( 隱藏元素,如果它包含特定的文本

我已經嘗試調整dystroy建議的代碼來隱藏另一頁上的DIV。

這是頁面HTML:

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

和以前一樣,如果它包含£0.00,我想隱藏“ct_pl_product_price”DIV

這是我嘗試過的腳本:

$(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的代碼),但它似乎不起作用。

有任何想法嗎?

你可以使用:contains在這種情況下:contains選擇器:

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

您可以使用: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()會稍快一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM