简体   繁体   English

jQuery load()的保证金问题

[英]Margin issue with jquery load()

I am loading html page inside a div with jquery. 我正在使用jQuery在div中加载html页面。 It does work fine. 它确实工作正常。

 var loginBtn = $("#loginBtn"); var loginPage = $("#login"); var submitBtn = $("#submitBtn"); var submitPage = $("#submit"); var checkBtn = $("#checkBtn"); var checkPage = $("#check"); loginPage.load( "login.html" ); submitPage.load( "submitPoints.html" ); checkPage.load( "checkPoints.html" ); 
 body { margin: 0 !important; padding: 0 !important; background-color: white; } #mainFrame { width: 100%; height: 300px; background-color:cadetblue; padding-top: 0; margin-top: 0px; position: relative; } 
 <div id="mainFrame"> <div id="login"></div> <div id="check"></div> <div id="submit"></div> </div> 

My issue is that if the loaded html has no content, the margin between the parent document body (white) and the top of the loaded html (green) is none (that's what I want, it's ok). 我的问题是,如果加载的html没有内容,则父文档正文(白色)和加载的html顶部(绿色)之间的边距为空(这就是我想要的,没关系)。

在此处输入图片说明

However as soon as I add content to the loaded html, a gap is generated at the top of the page :\\ 但是,一旦我将内容添加到已加载的html,页面顶部就会生成一个间隙:\\ 在此处输入图片说明

I thought it was all about setting some line-height prop in the css but it seems helpless. 我以为这就是在CSS中设置行高道具的原因,但这似乎无济于事。

Any ideas what I am doing wrong ? 有什么想法我做错了吗?

When you load new content it gets rendered in the document and those new elements might have properties. 加载新内容时,它将在文档中呈现,并且这些新元素可能具有属性。 In this case, most probably the Login has a margin value. 在这种情况下,登录名很可能具有边距值。 Another option is that it has a class or some selector that is being picked up by a CSS file which appends the margin to it. 另一个选择是,它具有一个类或某个选择器,该选择器由CSS文件拾取,并在其后附加空白。

Easiet way would be to right-click on the Login element, choose inspect, and analyze the style of the element with web-dev / style. 最简单的方法是右键单击Login元素,选择检查,然后使用web-dev / style分析元素的样式。

What you are seeing is the top margin of the first piece of content overflowing its container (also known more commonly as margin collapsing ): 您所看到的是第一部分内容溢出其容器的顶部边距(也通常称为边距折叠 ):

 body { background:yellow; } #container { background:green; height:300px; } 
 <div id="container"> <h1>I have a top margin of 1em by default that is overflowing into the body.</h1> </div> 

If you give your container element a padding of that same amount, the margin space of the body won't be used and the element will be pushed down in the green area. 如果为容器元素提供相同数量的padding ,则将不使用body的边距空间,并且该元素将在绿色区域中向下推。

 body { background:yellow; } #container { background:green; height:300px; padding:1em; } 
 <div id="container"> <h1>I have a top margin of 1em by default that is now contained within my parent.</h1> </div> 

Or, you could set the top margin of the first piece of content to zero: 或者,您可以将第一部分内容的上边距设置为零:

 body { background:yellow; } #container { background:green; height:300px; } #container > h1:first-child { margin-top:0; } 
 <div id="container"> <h1>My top margin has been set to zero.</h1> </div> 

Finally, you could set the overflow of the content area to auto but (although this seems to be the popular answer), I don't prefer this approach as you run the risk of unintended fitting of the content as the content changes and/or the container size changes. 最后,您可以将内容区域的溢出设置为auto但是(尽管这似乎是很流行的答案),但我不喜欢这种方法,因为随着内容的更改和/或内容的变化,您可能会意外地适应内容容器尺寸发生变化。 You give up a bit of sizing control: 您放弃了一些尺寸控制:

 body { background:yellow; } #container { background:green; height:300px; overflow:auto; } 
 <div id="container"> <h1>The content area has had its overflow set to auto.</h1> </div> 

If you want to keep the margin on the inner content, you should set an overflow. 如果要在内部内容上保留边距,则应设置一个溢出。 Look what happens when we remove the overflow: auto line from .content > div (try clicking the box after running the code sample below). 看看当我们消除overflow: auto时会发生什么overflow: auto .content > div overflow: auto行(在运行下面的代码示例后尝试单击框)。

This is because of margin collapsing . 这是因为保证金崩溃 The margin on the inner content is combined with the margin on the outer element and applied on the outer element, ie two margins of the two elements are collapsed into a single margin. 内部内容上的边距与外部元素上的边距合并并应用于外部元素,即,两个元素的两个边距折叠为一个边距。

 document.querySelector('.content').addEventListener('click', (e) => { e.target.classList.toggle('overflow'); }); 
 html, body { margin: 0; padding: 0; } .outer { width: 200px; background: red; } .content > div { width: 100%; height: 300px; background: cadetblue; cursor: pointer; } .content > div.overflow { overflow: auto; } .test { margin: 10px; display: block; } 
 <div class="outer"> <div class="content"> <div><span class="test">Test</span></div> </div> </div> 

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

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