如何使用jQuery找到具有display: none
的父元素?
.hidden-one
{
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hidden-one"> <!-- FIND AND SHOW THIS ONE -->
<div>...</div>
<div>...</div>
<div class="deeper">
<span class="start-here">Start here</span>
</div>
<div>...</div>
</div>
你需要迭代.start-here
的所有父级:
$('.start-here').parents().each(function() {
if ($(this).css('display') === 'none')
{
$(this).show();
}
});
.hidden-one
{
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hidden-one"> <!-- FIND AND SHOW THIS ONE -->
<div>...</div>
<div>...</div>
<div class="deeper">
<span class="start-here">Start here</span>
</div>
<div>...</div>
</div>
此代码也适用于具有style="display: none"
属性的元素。