将HTML代码段添加到HTML中?

时间:2014-01-18 13:05:25

标签: javascript jquery html

有没有办法将HTML从文件嵌入到另一个文件中?它可以用于制作页眉或页脚,而不需要在每个文件中粘贴相同的代码。

4 个答案:

答案 0 :(得分:2)

您可以使用jQuery代码调用它们,将其插入您的网页页面。

<head>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>     

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

</head>

您可以通过插入

在网页的任何位置展示它
 <div id="includedContent"></div>

答案 1 :(得分:1)

这适用于php:

<?php 
    include("header.html");
?>
<p>Here you can write your text</p>
<?php
    include("footer.html");
?>

这是最简单的工作

答案 2 :(得分:1)

如果您不想使用php,可以将grunt与twig生成器结合使用,您将在html页面中拥有Twig(例如:includes,layout inheritence等)的强大功能。 / p>

然后使用grunt build,您将生成一个包含所有决赛html页面的dist文件夹。

生成器示例:https://github.com/polem/generator-gruig

答案 3 :(得分:0)

您可能需要考虑在asp.net中使用母版页,或者使用iframes,其中页眉,页脚和iframe位于父级,内容将加载到子级iframe中。