HTML文件应如下所示:
<!--render header.html--> <div> Content </div> <!--render footer.html-->
我怎样才能做到这一点?
解决方法
另请注意,某些IDE会为您处理此事. Dreamweaver是一个例子;在ASP.NET中有master页面;等等.
PHP:
<?PHP require($DOCUMENT_ROOT . "path to file/include-file.html"); ?>
ASP:
<!--#include file="path to file/include-file.html"-->
JS:
JavaScript is another way to include HTML within the pages of your
site. This has the advantage of not requiring server-level
programming. But it’s a little more complicated than the server-level
include methods.
Save the HTML for the common elements of your site to a JavaScript
file. Any HTML written in this file,must be printed to the screen
with the document.write function.Use a script tag to include the
JavaScript file on your pages.
<script type=”text/javascript” src=”path to file/include-file.js”>- Use that same code on
every page that you want to include the file.
请注意,JS版本不是理想的.
JS可能在浏览器中被禁用或不可用.
2.该页面不会一次呈现/加载.
另外,我不认为这个DRY真的很重要.考虑使用将为您创建页面模板的IDE(例如Dreamweaver).
如果你有勇气(有点老式),而且您不能使用上述任何一种,请考虑为您的内容使用iframe:
<html> <body> <div>my header</div> <iframe src="mycontent.html" /> <div>my fooder</div> </body> </html>
免责声明我宁愿切断我自己的手,而不是执行iframe或JS方法.深入考虑您是否真的需要这样做.