Jquery:从文本节点获取文本
Jquery: get text from text node
我有很多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标记,使其更容易访问。然后我控制台记录它并得到"$0.00 $0.00 $0.00"(页面上的每个元素都有一个"$0.00")。但没有成功。
我就是不知道哪里出错了
你需要遍历每个父元素:
来自: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();
}
});
在if语句中只有
$(".ProductPriceRating span").text()
在console.log中,有
$(".ProductPriceRating em span").text()
这可能是问题所在,因为还有一个span的class "Rating"
相关文章:
- 需要使用javascript获取输入文本,然后将其添加到句子中
- 从ajax请求中获取原始文本
- 当运行JS函数时,如何在c#中的Edgejs中获取错误文本
- 如果所选内容在标记内,如何获取格式化文本
- 如何使用 JQuery 从相对元素获取 html 文本
- Meteor:从Twilio获取SMS文本列表,并将它们插入mongoDB
- 使用 jQuery 从选择标签中抓取文本
- jQuery 获取原始文本(未转义),以便通过下划线模板进一步解析
- 使用 jQuery 从目标页面而不是当前页面获取元素文本
- 用javascript获取svg文本值
- 如何在按子字符串更改后获取旧文本
- JavaScript:获取搜索文本在正文上的滚动位置
- 从p:inputText javascript获取值文本
- AJAX/JS:是否可以在没有提交按钮和刷新页面的情况下获取输入文本的值
- 使用JQUERY/Javascript获取DotNetNuke文本编辑器HTML值
- 使用 casperjs 抓取文本节点的最快方法
- 在Oracle Apex的javascript部分中获取富文本编辑器的HTML文本
- 如何在PHP中获取html文本框值
- Chrome扩展程序获取DOM文本并在弹出窗口中显示.html然后单击按钮
- 如何使用 ajax 实时搜索获取脚本文本框中的搜索值