Javascript Jquery:从文本节点获取文本
我有一堆这样的div,如果售价是0.00美元,我希望它们消失Javascript Jquery:从文本节点获取文本,javascript,jquery,Javascript,Jquery,我有一堆这样的div,如果售价是0.00美元,我希望它们消失 <div class="ProductPriceRating"> <em> <strike class="RetailPriceValue">$79.99</strike> $0.00 </em> <span class="Rating Rating0"> <img src="..
<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>
$79.99
$0.00
所以我写了这个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();
}
});
$(函数(){
$(“.ProductPriceRating em”).contents().filter(函数(){
返回this.nodeType!=1;
}).wrap(“”);
log($(“.ProductPriceRating em span”).text();
如果($(“.ProductPriceRating span”).text()=='$0.00'){
console.log('success');
$('div.ProductPriceRating').hide();
$('div.ProductActionAdd').hide();
}
});
我在文本节点周围包装了一个span标记,以便于访问。然后我将其记录下来,并获得“$0.00$0.00$0.00”(页面上每个元素一个“$0.00”)。但是没有成功
我只是不知道哪里出了问题您需要遍历每个父元素: 发件人:
if($(".ProductPriceRating span").text() == ' $0.00'){
console.log('success');
$('div.ProductPriceRating').hide();
$('div.ProductActionAdd').hide();
}
致:
在你的if声明中,你只有
$(".ProductPriceRating span").text()
在您的console.log中,您有
$(".ProductPriceRating em span").text()
这可能是个问题,因为类“Rating”也有一个span,因此您不再试图从textnode获取文本,您从编写问题标题时就将其包装在span中,现在您正在尝试做一些完全不同的事情?
$(".ProductPriceRating em span").text()