如何根据父div的attribute
值隐藏子div。我有以下标记:
<div class="offerContainer">
<div class="Offers" producttype="CIP">
<div class="buttons_wrap"></div>
</div>
<div class="Offers" producttype="P">
<div class="buttons_wrap"></div>
</div>
<div class="Offers" producttype="CIP">
<div class="buttons_wrap"></div>
</div>
</div>
我只想在buttons_wrap
的属性值为“CIP”时隐藏producttype
。
类似的东西:
if($(obj).parents(".offersContainer").find('.Offers').attr('producttype').value == "CIP")
$('.buttons_wrap').hide();
答案 0 :(得分:3)
$('.buttons_wrap').each(function(){
if($(this).parents('.Offers').attr('producttype') == "CIP"){
$(this).hide();
}
});
JSFIDDLE示例:
答案 1 :(得分:0)
$(function() {
var $offers = $('.Offers');
$offers.each(function() {
var productType = $(this).attr('producttype');
if(productType != "CIP")
{
$(this).children().hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div class="offerContainer">
<div class="Offers" producttype="CIP">
<div class="buttons_wrap"><button>First</button></div>
</div>
<div class="Offers" producttype="P">
<div class="buttons_wrap"><button>Second</button></div>
</div>
<div class="Offers" producttype="CIP">
<div class="buttons_wrap"><button>Third</button></div>
</div>
</div>