繁体   English   中英

如何在另一个HTML文件中包含HTML文件

[英]How to include html file in another html file

我有两个html文件first.htmlsecond.html ,我想将第二个文件包含在第一个文件中以实现代码可重用性。 我正在使用以下语句来包含文件:

<link rel="import" href="html file name here">

但这是行不通的。 我也尝试使用其他解决方案,但没有一个起作用。

因此,提出任何建议。

使用jQuery:

a.html:

<html> 
  <head> 
<script src="jquery.js"></script> 
<script> 
$(function(){
  $("#content").load("b.html"); 
});
</script> 
 </head> 

 <body> 
 <div id="content"></div>
</body> 
</html>

b.html:

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

看这个网站。 http://www.html5rocks.com/zh-CN/tutorials/webcomponents/imports/

它建议什么是最佳做法。 它始终取决于您的实际需求。 为什么不使用镜框?

我认为最好的解决方案是:

使用jQuery:

a.html:

<html> 
  <head> 
    <script src="jquery.js"></script> 
    <script> 
    $(function(){
      $("#includedContent").load("b.html"); 
    });
    </script> 
  </head> 

  <body> 
     <div id="includedContent"></div>
  </body> 
</html>
b.html:

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

这样,我可以得到一个简单干净的解决方案。 http://api.jquery.com/load/

来源: 在HTML文件中包含另一个HTML文件 Google是您的朋友:)

使用jquery的.load()函数加载模板。 请参阅此文档。 http://api.jquery.com/load/

暂无
暂无

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

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