多個HTML頁面公用頭部尾部

首先我們需要創建好我們需要的頭部與尾部:

        head.js:

document.writeln("<!DOCTYPE html>");
document.writeln("<html lang=\'en\'>");
document.writeln("<head>");
document.writeln("	<meta charset=\'UTF-8\'>");
document.writeln("	<title>head</title>");
document.writeln("</head>");
document.writeln("<body>");
document.writeln("	<div style=\'background-color:green;width:200px;height:200px;margin:auto;\'>我是頭部</div>");
document.writeln("</body>");
document.writeln("</html>");

        foot.js:

document.writeln("<!DOCTYPE html>");
document.writeln("<html lang=\'en\'>");
document.writeln("<head>");
document.writeln("	<meta charset=\'UTF-8\'>");
document.writeln("	<title>foot</title>");
document.writeln("</head>");
document.writeln("<body>");
document.writeln("	<div style=\'background-color:yellow;width:200px;height:200px;margin:auto;\'>我是尾部</div>");
document.writeln("</body>");
document.writeln("</html>");

        tips:我們可以將寫好的html代碼通過網站轉化成我們需要的js代碼 http://tool.chinaz.com/Tools/Html_Js.aspx

        例:

 

然後在需要用到這個頭、尾部的頁面通過以下形式調用即可:

<script type="text/javascript" src="head.js"></script>

 

效果:

        index.html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>body</title>
</head>
<body>
	<script type="text/javascript" src="head.js"></script>
	<div style="background-color:red;width:200px;height:200px;margin:0 auto;">我是body</div>
	<script type="text/javascript" src="foot.js"></script>
</body>
</html>

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章