我在div循环中遇到特定div的麻烦。
下面的代码是在页面上多次重复的单个项目。如果带有edd_price类的span包含文本'Free',我想隐藏这个项目的div类'vat'。
<div class="product-thumb">
<a href="http://tb.eldo.co.uk/downloads/achieving-sales-on-the-telephone-sample-notes/">
<div class="thum">
<img width="185" height="150" src="blah"/>
</div>
<div class="title">Title goes here</div>
<div class="price">
<span class="edd_price">Free</span>
</div>
<div class="vat clear">
price excluding 20% vat
</div>
</a>
我尝试过的所有内容都不会影响页面上的任何内容或所有项目,这是我认为应该起作用的内容 -
$(document).ready(function() {
if ($(".edd_price").contains("Free")) {
$(this).parent().next('div.vat').hide();
}
});
答案 0 :(得分:2)
首先你的条件不起作用,contains方法应该是这样的:
if ( $(".edd_price:contains(free)"))
看看这里:http://www.w3schools.com/jquery/sel_contains.asp
条件是你必须再次获得该元素,因为你的这个不是他。这不是回调。然后:
$(function() {
if ( $(".edd_price:contains(free)")) {
$('.edd_price:contains("Free")').parent().next('.vat').hide();
}
});
以下是演示: https://jsfiddle.net/87qcfng8/
Ps:你的第一个div没有关闭,所以关闭它。
答案 1 :(得分:1)
条件是:
if ( $(".edd_price:contains(free)"))
并在里面使用:
$('.edd_price:contains("Free")').parent().next('.vat').hide();
答案 2 :(得分:0)
您需要获取“Free”容器的父节点(使用:contains("Free")
选择器检索)并使用类.vat
隐藏下一个元素:
$('.edd_price:contains("Free")').parent().next('.vat').hide();
这是一个演示:
$('.edd_price:contains("Free")').parent().next('.vat').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product-thumb">
<a href="http://tb.eldo.co.uk/downloads/achieving-sales-on-the-telephone-sample-notes/">
<div class="thum">
<img width="185" height="150" src="http://lorempixel.com/185/150"/>
</div>
<div class="title">Title goes here</div>
<div class="price">
<span class="edd_price">Free</span>
</div>
<div class="vat clear">
price excluding 20% vat
</div>
</a>