隐藏除单嵌套Div以外的所有网页项目

发布于 2024-12-04 12:54:27 字数 1377 浏览 1 评论 0原文

假设我的网页有这样的结构:

<body>
    <div id="fee">
        <div id="fi">

            <div id="actual_content">

                <p>Content</p>
                <div id="some_important_stuff">Blah</div>
                <p>More content</p>
                <span class="and_another_thing">Meh</span>

                ...
            </div>

            <div id="fo>
                ...
            </div>

            ...
        </div>

        <div id="fum">
            ...
        </div>

        ...
    </div>

    <div id="fazz">
        ...
    </div>

    ...
</body>

我想创建一个打印 CSS 样式,隐藏除 actual_content 内容之外的所有内容。

我的第一次尝试是这样的:

body * {
    display: none; /* Hide everything first */
}

/* Show content div and all of its ancestors */

body > #fee {
    display: block;
}

body > #fee > #fi {
    display: block;
}

body > #fee > #fi > #actual_content {
    display: block;
}

/* Unhide contents of #actual_content */

#actual_content * {
    display: block; /* Not ideal */
}

但是,由于没有“display: auto”或“display: default”,当我尝试取消隐藏 actual_content 项目时,我弄乱了它们的样式。我也不喜欢对 actual_content 的路径进行硬编码,因为它可能会发生变化。

Suppose my web page has a struture like this:

<body>
    <div id="fee">
        <div id="fi">

            <div id="actual_content">

                <p>Content</p>
                <div id="some_important_stuff">Blah</div>
                <p>More content</p>
                <span class="and_another_thing">Meh</span>

                ...
            </div>

            <div id="fo>
                ...
            </div>

            ...
        </div>

        <div id="fum">
            ...
        </div>

        ...
    </div>

    <div id="fazz">
        ...
    </div>

    ...
</body>

I want to create a print CSS style that hides everything except for the contents of actual_content.

My first attempt was like this:

body * {
    display: none; /* Hide everything first */
}

/* Show content div and all of its ancestors */

body > #fee {
    display: block;
}

body > #fee > #fi {
    display: block;
}

body > #fee > #fi > #actual_content {
    display: block;
}

/* Unhide contents of #actual_content */

#actual_content * {
    display: block; /* Not ideal */
}

However, since there's no "display: auto" or "display: default", I mess up the styles of actual_content's items when I try to unhide them. I also don't like hard coding the path to actual_content since it might change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮云落日 2024-12-11 12:57:02

我知道您的标签表明您需要 CSS 解决方案,但 PrintArea jQuery 插件非常适合您的需求:

PrintArea发送指定的dom元素到打印机,维护
应用于正在打印的元素的原始 CSS 样式。

http://plugins.jquery.com/project/PrintArea

I know your tags show you want a CSS solution, but the PrintArea jQuery plugin is perfect for your needs:

PrintArea sends a specified dom element to the printer, maintaining
the original css styles applied to the element being printed.

http://plugins.jquery.com/project/PrintArea

幽蝶幻影 2024-12-11 12:57:00

您需要通过设置 display:none 进入并定位您不想单独显示的所有内容。如果您可以更改类名称等,则应该取消嵌套 actual_content

,然后将 hide_div 等类添加到其他

中。 这样它们就很容易关闭。

You'll need to go in and target everything that you don't want to show up individually by setting display:none. If you can change class names, etc, you should un-nest the actual_content <div>, then add classes like hide_div to the other <div>s so they're easy to turn off.

你的他你的她 2024-12-11 12:56:57

在顶层 div 设置 visibility:hidden,然后在您想要设置 visibility:visible 的 div 上

on the top level div set visibility:hidden, then on the div you want set visibility:visible

溺深海 2024-12-11 12:56:54

您可能想要使用 可见性 属性。 W3Schools 描述了显示属性和可见性属性之间的区别:可见性属性会影响元素的可见性,但不会影响其结构,即它通常在页面上占据的空间。

You probably want to use the visibility property. W3Schools describes the difference between the display and visibility properties: the visibility property affects the visibility of an element without affecting its structure, i.e. the space it would normally occupy on the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文