Jquery:从文本节点获取文本

时间:2014-05-01 21:41:23

标签: javascript jquery

我有一堆这些div,如果售价为0.00美元,我希望它们消失

<div class="ProductPriceRating">
    <em>
         <strike class="RetailPriceValue">$79.99</strike> 
         $0.00
    </em>
    <span class="Rating Rating0">
        <img src="..." alt="" style="display: none" />
    </span>
</div>

所以我写了这个jquery

$(function(){
    $(".ProductPriceRating em").contents().filter(function() {
        return this.nodeType != 1; 
    }).wrap("<span></span>");
    console.log($(".ProductPriceRating em span").text());
    if($(".ProductPriceRating span").text() == ' $0.00'){
            console.log('success');
        $('div.ProductPriceRating').hide();
        $('div.ProductActionAdd').hide();
    }
});

我在文本节点周围包装了一个span标记,以便于访问。然后我控制台登录并获得&#34; $ 0.00 $ 0.00 $ 0.00&#34; (一个&#34; $ 0.00&#34;对于页面上的每个元素)。但没有成功。

我只是不知道我哪里出错了

2 个答案:

答案 0 :(得分:2)

您需要遍历每个父元素:

来自:

if($(".ProductPriceRating span").text() == ' $0.00'){
        console.log('success');
    $('div.ProductPriceRating').hide();
    $('div.ProductActionAdd').hide();
}

要:

$(".ProductPriceRating").each(function() { 
    if($(this).find('span').text() == ' $0.00'){
            console.log('success');
        $(this).hide();
        $('div.ProductActionAdd').hide();
    }
});

答案 1 :(得分:0)

在你的if语句中,你只有

$(".ProductPriceRating span").text()

在您的console.log中,您有

$(".ProductPriceRating em span").text()

这可能是问题所在,因为还有“等级”等级的范围