如何在一个html文件里包含或引用另一个html文件,本文章介绍二种方法。

 

第一种方法:jquery方法

jquery代码:

< >
  $(function(){
    var includes = $(\'[data-include]\');
    jQuery.each(includes, function(){
      var file = \'views/\' + $(this).data(\'include\') + \'.html\';
      $(this).load(file);
    });
  });
</ >

html代码:

<div data-include=\"header\"></div>
<div data-include=\"footer\"></div>

以上代码实现了将views/header.html 和 views/footer.html包含加载到当前html文件中。

 

第二种方法:jquery load方法

a.html

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

  <body> 
     <div id=\"includedContent\"></div>
  </body> 
</html>

b.html:

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

以上代码将b.html文件包含到a.html

收藏 打印