隐藏元素,如果它包含特定的文本 [英] Hide element if it contains specific text

查看:101
本文介绍了隐藏元素,如果它包含特定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的电子商务网站上有以下代码。



我想搜索 以检查其是否包含0.00。 b
$ b

如果它包含0.00,我想隐藏包含价格的父div



这是因为我们希望产品在线显示但即使它无法购买,价格仍然显示,所以我们不希望人们看到价格为0.00英镑时感到困惑。

 < div class =ct_pd_item_price ct_pd_item_value> 
< span itemprop =price>
< span class =ct_currencyCode> GBP< / span>
< span class =ct_currencySymbol>£< / span> 0.00< / span>
< / div>

这可能使用某种形式的javascript吗?

解决方案

如果你不能改变你的HTML,这有点乏味,因为文本必须从currencyCode和currencySymbol清除。一种方法是克隆div,然后获取文本:
$ b

  $( ().ct_pd_item_price.ct_pd_item_value)。filter(function(){
return $(this).clone()。find('.ct_currencyCode,.ct_currencySymbol')。remove().end().text()。 trim()==0.00
})。hide()

示范



当然,数据属性中的值将更容易处理。


I have the following code on my ecommerce website.

I would like to search to check whether it contains "0.00"

If it does contain "0.00" I would like to hide the parent div containing the price

This is because we want the product to appear online but even though it cannot be purchased, the price is still displayed, so we don't want people to be confused if they see the price as £0.00.

<div class="ct_pd_item_price ct_pd_item_value">
<span itemprop="price">
<span class="ct_currencyCode">GBP</span>
<span class="ct_currencySymbol">£</span>0.00</span>    
</div>

Is this possible using some form of javascript?

解决方案

If you can't change your HTML, it's a little tedious because the text must be cleand from the currencyCode and currencySymbol. A way to do this is to clone the div and then take the text :

$(".ct_pd_item_price.ct_pd_item_value").filter(function(){
   return $(this).clone().find('.ct_currencyCode, .ct_currencySymbol').remove().end().text().trim()=="0.00"
}).hide()

Demonstration

Of course a cleaner HTML, maybe with the value in a data attribute, would be easier to deal with.

这篇关于隐藏元素,如果它包含特定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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