【问题标题】:Find hidden parent element jQuery查找隐藏的父元素jQuery
【发布时间】:2019-01-30 06:10:26
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery iteration hidden display


    【解决方案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" 属性的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多