如果我们想在多个页面上运行相同的 html header 代码,是否可以使用外部 html 文件或链接到现有的 header 代码,而不是在每个页面中编写相同的代码?
最佳答案
你可以用 jquery 来做到这一点。将此代码放入index.html
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function(){
$("#header").load("header.html");
$("#footer").load("footer.html");
});
</script>
</head>
<body>
<div id="header"></div>
<!--Remaining section-->
<div id="footer"></div>
</body>
</html>
并将此代码放入 header.html 和 footer.html 中,与 index.html 位于同一位置
<a href="http://www.google.com">click here for google</a>
然后访问index.html,您应该能够单击链接标签。
关于javascript - 如何创建可包含在其他 HTML 页面中的 HTML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32022146/