隐藏div,但不隐藏嵌套的div [英] Hide div, without hiding it's nested divs

查看:83
本文介绍了隐藏div,但不隐藏嵌套的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题,但是我似乎在stackoverflow的任何地方都找不到答案.

This is a simple question, but i can't seem to find the answer anywhere on stackoverflow.

我有以下代码段:

<div>//The div to be hidden
    <div></div>//I don't want to hide this div
    <div></div>//Nor this one
</div>

我要这样做的原因是,当我隐藏div时,我使用其标记名将其隐藏,但是当我显示它们时,则使用其类名.因此,innerdiv或嵌套div"不会再次出现.

The reason why i wanna do this is because, when i hide the divs, i hide them using their tagname, but when i show them, i use their classname. Therefore the innerdivs or the "nested divs" doesn't show up again.

之前:

<div>
    <div></div>
    <div></div>
</div>
<div></div>
<div>
    <div></div>
    <div></div>
</div>

之后:

<div style="display:none;">
    <div></div>
    <div></div>
</div>
<div style="display:none;"></div>
<div style="display:none;">
    <div></div>
    <div></div>
</div>

推荐答案

我假设提供的代码在另一个元素内.我将其称为div#root

I'm assuming that the code provided is inside another element. I'm going to call this div#root

<div id="root">
    <div>
        <div></div>
        <div></div>
    </div>
    <div></div>
    <div>
        <div></div>
        <div></div>
    </div>
</div>

要隐藏#root的所有直接div子级,可以使用选择器#root > div.

To hide all immediate div children of #root, you can use the selector #root > div.

因此,您的jQuery可能看起来像$('#root > div').hide()

So, your jQuery might look like $('#root > div').hide()

这是您要做什么吗?

这篇关于隐藏div,但不隐藏嵌套的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆