简体   繁体   English

如何隐藏嵌套了div的父div

[英]How to hide a parent div which has nested div inside

HTML: HTML:

<div class="btn-small blue">
    <span class="label bottom">Vascular Surgery</span>
</div>

<div class="btn-small red">
    <span class="label bottom">Administrator</span>
</div>

<div class="btn-small blue">
    <span class="label bottom">Cardiology North</span>
</div>

<div class="calContent">
    <div class="divContent">
        <div class="divContentHolder">
            <div>
                VASCULAR CURGERY
            </div>
            <div>
                CALENDAR GOES HERE
            </div>
        </div>
    </div>
    <div class="divContent">
        This is Second
    </div>
    <div class="divContent">
        This is Third
    </div>
</div>

Fiddle: https://jsfiddle.net/geetof8x/ 小提琴: https : //jsfiddle.net/geetof8x/

If I remove the following code: 如果我删除以下代码:

<div class="divContent">
    <div class="divContentHolder">
        <div>
            VASCULAR CURGERY
        </div>
        <div>
            CALENDAR GOES HERE
        </div>
    </div>
</div>

With: 带有:

<div class="divContent">
    This is First
</div>

The script works fine but with the added extra div inside the divContent div, the code doesn't work correctly. 该脚本可以正常工作,但是在divContent div中添加了额外的div时,代码无法正常工作。

The removed code will be used in the final template. 删除的代码将在最终模板中使用。 How can I edit the javascript so it can handle either case scenario. 我该如何编辑JavaScript,使其可以处理两种情况。

You're only interested in the children (not grandchildren, etc.) of .calContent . 您只对.calContent子项 (而不是孙子项等) .calContent

One solution is to use the child selector to limit your selection to only the direct children: 一种解决方案是使用子选择器将您的选择限制为仅直接子:

$('.calContent > div:eq(' + $(this).index() + ')').fadeOut();

Updated Fiddle 更新小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM