找到一種方法可以做到這一點從html5rocks,但它遠遠不可讀。這是理想的方式:
<body>
<include src="header.html"/>
</body>
這是它是如何通過HTML5進口實施:
<head>
<link rel="import" href="header.html">
</head>
<body>
...
<script>
document.body.appendChild(
document.querySelector('link[rel="import"]')
.import
.querySelector('body')
.cloneNode(true));
</script>
</body>
注:
- 不清楚如何選擇包括如果兩個
header.html
和footer.html
是否有
querySelector('body')
需要避免Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node': Nodes of type '#document' may not be inserted inside nodes of type 'BODY'.
- 不清楚如何插入
<body>
標籤內容,而不標籤本身
- HTML5進口不工作在Firefox(38)http://caniuse.com/#feat=imports =/
你看http://www.w3schools.com/ tags/tag_iframe.asp – Garry
這是實驗性的。你可能不得不採取像http://api.jquery.com/load/ –