2014-05-01 35 views
0

我有一堆这些div的,我希望他们消失,如果出售价格为$ 0.00jQuery的:从文本节点文本

<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标记以便于访问。然后,我将控制台记录下来,并获得“$ 0.00 $ 0.00 $ 0.00”(页面上每个元素的一个“$ 0.00”)。但没有成功。

我只是不知道我要去哪里错了

+2

所以你不再试图获得一个跨度来自textnode的文本,你从问题标题写出的时间跨度中包含了它,现在你正在尝试做一些完全不同的事情? – adeneo

回答

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(); 
    } 
}); 
0

在您的if语句中,您只有

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

在你的console.log,你有

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

这可能是问题,因为还没有与类“等级”