简体   繁体   English

jQuery hide() 目标<p>元素隐藏整个父div的背景</p>

[英]jQuery hide() targeting <p> element hides background of entire parent div

I have a simple blog page - a list of posts that each consist of a title and contents.我有一个简单的博客页面 - 一个包含标题和内容的帖子列表。 When the page loads I want all posts' contents hidden until their titles are clicked.当页面加载时,我希望隐藏所有帖子的内容,直到点击它们的标题。 The following code accomplishes this but with an unwanted side effect - the on-page-load hide() function that hides each post's content also hides the background of the containing (id="content") div:以下代码实现了这一点,但有一个不需要的副作用 - 隐藏每个帖子内容的页面加载 hide() function 也隐藏了包含 (id="content") div 的背景:

Relevant JavaScripts :相关的 JavaScript

$(document).ready(function() {

    $(".blog_post p").hide();

    //BLOG CONTENT ANIMATION
    $('.blog_post').click(function() {
        $(this).find('p').slideToggle(130);
    });      
});

Summary of blog page :博客页面摘要

<section class="grid_7">
<div id="content">

    <div class="blog_post">

        <div class="blog_head">
            <h2>Title</h2>
        </div>

        <p>Contents</p>

    </div>

</div>
</section>

Relevant CSS :相关 CSS

section { 
    border: 1px solid white;
}

#content {
    margin: 20px;
    background-image:url('../images/content_background.jpg');
}

When the page loads the list of titles displays without the #content parent div's background.当页面加载标题列表时显示没有#content 父 div 的背景。 However when I click on a post's title the #content div's background shows up behind all posts up to and including that one.但是,当我单击帖子的标题时,#content div 的背景会显示在所有帖子的后面,包括该帖子。

Any idea what's going on?知道发生了什么吗?

It sound like you have some CSS that applies to the blog_head elements, that makes them float, for example:听起来您有一些适用于 blog_head 元素的 CSS ,这使它们浮动,例如:

.blog_post { float: left; }

In that case, the reason that the background doesn't show up is that the height of the content div is zero.在这种情况下,背景不显示的原因是内容 div 的高度为零。 A floating element doesn't affect the size of its parent, and when the content div only contains the headers, the height becomes zero.浮动元素不影响其父元素的大小,当内容 div 仅包含标题时,高度变为零。 The background is still there, but there is no area where it's visible.背景仍然存在,但没有可见的区域。

Add an overflow to the content div, that will make it contain its children:向内容 div 添加溢出,这将使其包含其子项:

#content { overflow: hidden; }

Note that this will not hide anything as long as you don't specify a size for the content element, it will just change how it's rendered so that it will become a container for its children.请注意,只要您不为内容元素指定大小,这不会隐藏任何内容,它只会更改其呈现方式,使其成为其子元素的容器。

A bit of a stab in the dark: Your #content div will, of course, be a lot shorter as the blog posts aren't there, basically consisting just of the div s with the titles.在黑暗中有点刺:当然,您的#content div会短很多,因为博客文章不存在,基本上只包含带有标题的div Perhaps that's the problem.也许这就是问题所在。

Does the image have a blank (or subtle) bit at the top or something, so that it's only apparent that it's there when there's more content in the #content div (eg, when it's taller)?图像顶部是否有空白(或微妙)位或其他东西,所以只有当#content div中有更多内容时(例如,当它更高时),它才明显存在? Or is there some other reason you can see that when #content is really short, you wouldn't see the background on the part of it that's there?或者是否有其他原因可以让您看到当#content真的很短时,您看不到它那部分的背景? (You can use the debugging tools in most modern browsers to see what the dimensions of the #content div are when the paragraphs are hidden; or slap a border on it temporarily, but tools these days are pretty good.) (您可以使用大多数现代浏览器中的调试工具来查看隐藏段落时#content div 的尺寸;或者暂时在其上添加边框,但这些天的工具非常好。)

Basically, since the jQuery doesn't, of course, actually hide the background, it must be a side-effect of the paragraphs being hidden — because of the effect that has on the dimensions of the #content div .基本上,由于 jQuery 当然实际上并没有隐藏背景,因此它一定是隐藏段落的副作用——因为对#content div的尺寸有影响。

This is working fine for me:这对我来说很好用:

HTML: HTML:

    <div class="blog_post">

        <div class="blog_head">
            <h2>Title</h2>
        </div>

        <p>Contents</p>

    </div>

</div>

CSS: CSS:

section { 
    border: 1px solid white;
}

#content {
    margin: 20px;
    background-image:url('http://bluebackground.com/__oneclick_uploads/2008/04/blue_background_03.jpg');
}

JS JS

$(document).ready(function() {

    $(".blog_post p").hide();

    //BLOG CONTENT ANIMATION
    $('.blog_post').click(function() {
        $(this).find('p').slideToggle(130);
    });      
});

Check it live here: Jsfiddle example在这里查看: Jsfiddle 示例

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

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