实际上有几十种方法可以做这种事情。您可以使用各种服务器端语言中的任何一种重写您的网站,无论是像 PHP 这样的脚本语言还是像 C# 或 Java 这样的编译语言。
不过,您不必那么激烈。您可以通过在页面中包含参考来简化它。这可以是直接使用另一个 HTML 文件,也可以是 JavaScript 文件。下面的链接显示了如何使用 JQuery 和 JavaScript 进行 HTML 文件链接。 JavaScript 并不难,它可以从您的所有页面中引用,因此它可以在一个文件中包含您想要更改的所有内容,允许您更改一个文件并反映在您的所有其他页面上。
HTML 包含在 JQuery 中:(如果您还没有使用 JQuery,我会犹豫使用整个库来完成一个可以在 vanilla JS 中轻松完成的功能)
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>
或 JavaScript:
a.html:
<html>
<body>
<h1>Put your HTML content before insertion of b.js.</h1>
...
<script src="b.js"></script>
...
<p>And whatever content you want afterwards.</p>
</body>
</html>
b.js:
document.write('\
\
<h1>Add your HTML code here</h1>\
\
<p>Notice however, that you have to escape LF's with a '\', just like\
demonstrated in this code listing.\
</p>\
\
');
Include another HTML file in a HTML file
如果您已经在使用 PHP,那么您很有可能可以通过从文本文件中读取并将 HTML 文本插入到您正在生成的页面中来使用您已经在使用的 PHP。
<?php
// do php stuff
include('fileOne.html');
// or //
readfile('fileTwo.html');
?>
how to embed html files in php code?