简体   繁体   English

JQuery将页面加载到带有样式的div中

[英]JQuery load page into div with styles

I am trying to use .load so I can load an external page into a div on the current user's page. 我正在尝试使用.load因此我可以将外部页面加载到当前用户页面上的div中。 When I call .load , does it load the content and then style it with the current stylesheet defined in that page? 当我调用.load ,是否会加载内容,然后使用该页面中定义的当前样式表对其进行样式设置? If not how would I go about doing that? 如果不是,我会怎么做呢? Example; 例;

<head>
  <link ref="stylesheet" href="main.css"/>
</head>
<body>
  <div class="section">
    <h2>Say this div was loaded with .load after page loaded</h2>
  </div>
</body>

If .section was loaded via .load , would the style be loaded of the current page and modify that div after it loaded or is it just the html that got loaded with no styling. 如果.section是通过.load加载的,那么样式是否会加载当前页面并在加载后修改该div,或者只是加载了没有样式的html。 If it is the latter, how would I style it without using <style> tags? 如果是后者,如何在不使用<style>标签的情况下设置<style>

Anytime you alter the DOM in a way that causes a redraw (such as adding visible elements to the DOM), each element is checked against all the rules in the CSS style-sheets. 只要您以导致重绘的方式更改DOM(例如向DOM添加可见元素),就会根据CSS样式表中的所有规则检查每个元素。

So to answer your question directly. 所以直接回答你的问题。 Yes, the styles will be added to elements that are added to the DOM at anytime. 是的,样式将添加到随时添加到DOM的元素中。

If the css is available on the page which contains section div then the styles will be applied after load renders the content. 如果css在包含section div的页面上可用,则在load呈现内容后将应用样式。 Also if the styles are part of the load response then also it will be rendered with styles applied. 此外,如果样式是load响应的一部分,那么它也将使用应用的样式进行渲染。

If the styles are not available on the page and also you are not getting it in the load response then you have to explicitly get it using ajax or adding a link tag with appropriate stylesheet url into the page. 如果页面上没有样式,并且您没有在加载响应中获取它,则必须使用ajax显式获取它,或者在页面中添加带有相应样式表URL的链接标记。

您当前页面的样式将应用于通过.load加载的内容

您可以在主.css文件中指定CSS,因此在添加DOM时,样式将从主CSS文件中获取。

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

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