简体   繁体   English

将html加载到div中,而不更改页面的其余部分

[英]Load html into div without changing the rest of page

So I've seen tons of people asking how to load html into a div, and my code does that fine....but then the rest of the page changes when I load the html into the div. 因此,我已经看到无数人问如何将html加载到div中,而我的代码可以做到这一点。...但是当我将html加载到div中时,页面的其余部分就会改变。

I have a page a.html that looks like this, and loads b.html 我有一个看起来像这样的页面a.html,并加载b.html

a.html a.html

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(function() {
$("#includedContent").load("b.html"); 
}); 
</script> 



 <div id="includedContent"></div>
 <h1>This is why I rule</h1>
</head>
</html>

then b.html looks like this 然后b.html看起来像这样

<p> This is my include file </p>

What happens is a.html loads and I can see This is why I rule momentarily, but then the code goes out and gets b.html. 发生的是a.html加载,我可以看到This is why I rule立即进行This is why I rule ,但是随后代码消失并得到b.html。 When b.html loads I can ONLY see This is my include file and the 'This is why I rule' message disappears... 加载b.html时,我只能看到This is my include file ,并且“这是我统治的原因”消息消失了...

Why is this? 为什么是这样? How do I prevent it? 我该如何预防? It does this on all browsers I have tested it on. 我在所有测试过的浏览器上都执行此操作。

You have to write your html code into a <body> </body> 您必须将html代码写入<body> </body>

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(function() {
$("#includedContent").load("b.html"); 
}); 
</script> 
</head>

<body>
 <div id="includedContent"></div>
 <h1>This is why I rule</h1>
</body>
</html>

All your HTML markup is in the head section not the body 您所有的HTML标记都在head而不是body

try this: 尝试这个:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

<script> 
$(function() {
$("#includedContent").load("b.html"); 
}); 
</script> 

</head>
<body>
     <div id="includedContent"></div>
     <h1>This is why I rule</h1>
</body>
</html>

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

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