查找隐藏的父元素jQuery

时间:2018-08-23 19:26:22

标签: javascript jquery iteration hidden display

如何使用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>

1 个答案:

答案 0 :(得分:1)

您需要迭代.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"属性的元素。