1、采用js方法
html中静态包含公共部分 - 宅男 - 宅男

html中静态包含公共部分 - 宅男 - 宅男

2、采用apache等服务器,使用include
apache默认不支持SSI,可以通过修改配置文件来使其支持

搜索AddType text/html .shtml的位置,打开以下注释

AddType text/html .shtml

AddOutputFilter INCLUDES .shtml

同时需要找到Options Indexes FollowSymLinks,在其后添上Includes

以上表示支持shtml的include命令,如果还想支持html,则需改为

AddType text/html .shtml .html

AddOutputFilter INCLUDES .shtml .html

使用办法:

建立A.html文件如下

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>include</title>
</head>
<body><!--#include file="B.html" --></body></html>

建立B.html文件如下

<div>我是被include包含进来的</div>