如何将一个 HTML 文件包含到另一个文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7542872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include one HTML file into another?
提问by Sahil Grover
I have tried every possible question here on stackoverflow but unable to resolve this issue ...
我已经在 stackoverflow 上尝试了所有可能的问题,但无法解决这个问题......
<!--#include virtual="include.shtml"-->
<!--#include virtual="include.html"-->
<!--#include file="include.shtml"-->
<!--#include file="include.html"-->
<!--#include virtual="/include.shtml"-->
<!--#include virtual="/include.html"-->
<!--#include file="/include.shtml"-->
<!--#include file="/include.html"-->
<? include("/include.shtml"); ?>
<? include("include.shtml"); ?>
<? include("/include.html"); ?>
<? include("include.html"); ?>
I tried with apache server running at localhost/include/index.html or file:///home/sahil/Desktop/include/index.html with all above includes but none of them is working for me :( .Now which method should i use to include one HTML file into another , considering my index.html and include.html both are in same directory ???
我尝试在 localhost/include/index.html 或 file:///home/sahil/Desktop/include/index.html 上运行 apache 服务器,其中包含上述所有内容,但没有一个对我有用:(。现在应该使用哪种方法我过去常常将一个 HTML 文件包含到另一个文件中,考虑到我的 index.html 和 include.html 都在同一目录中???
回答by Quentin
The former syntax is SSI, the latter is PHP. Both are server technologies and will only work if accessed from an HTTP server that supports and is configured to check the file for the syntax (which usually means you have to turn the support on and use a .shtml/.php file extension (or change the config from the default to determining which files to check)). Other server side technologies are available.
前者语法是SSI,后者是PHP。两者都是服务器技术,只有在从支持并配置为检查文件语法的 HTTP 服务器访问时才有效(这通常意味着您必须打开支持并使用 .shtml/.php 文件扩展名(或更改从默认配置到确定要检查的文件))。其他服务器端技术也可用。
The only "include" mechanisms in HTML itself are (i)framesand objects.
You could also consider a template system such as TTthat you could run as a build step to generate static HTML documents (NB: TT can also be used on the fly).
您还可以考虑使用模板系统,例如TT,您可以将其作为构建步骤运行以生成静态 HTML 文档(注意:TT 也可以即时使用)。
回答by Alerty
HTML is Static
HTML 是静态的
It is not possible to include a HTML page within another HTML page with static HTML because it is simply not supported by HTML. To make your HTML dynamic, you have 2 possibilities: use a client side script or use a server side technology.
不可能将一个 HTML 页面包含在另一个具有静态 HTML 的 HTML 页面中,因为 HTML 根本不支持它。要使您的 HTML 动态化,您有两种可能性:使用客户端脚本或使用服务器端技术。
...Unless you start using frames
(dropped with the HTML5 standard) or iframes
that are most likely not a solution because of the fact that it is treated as a completely different web page.
...除非您开始使用frames
(与 HTML5 标准一起删除)或者iframes
很可能不是解决方案,因为它被视为完全不同的网页。
Client Solution
客户解决方案
You could create a JavaScript file and write your HTML with the document.write
functionand then include the JavaScript file where ever you need it. Also, you could use some AJAX for this, but there are JavaScript libraries out there that could ease your burden such as the popular jQuerylibrary.
您可以创建一个 JavaScript 文件并使用该document.write
函数编写 HTML,然后将 JavaScript 文件包含在您需要的任何位置。此外,您可以为此使用一些 AJAX,但有一些 JavaScript 库可以减轻您的负担,例如流行的jQuery库。
Yet, I would not suggest using a client solution because it is more trouble than it is worth...
然而,我不建议使用客户端解决方案,因为它比它的价值更麻烦......
Server Solution
服务器解决方案
There are many server side solutions out there: ASP, ASP.NET, ASP.NET MVC, Java, PHP, Ruby and the list goes on. What you need to understand is that a server a side technology could be described as a parser for a specific server side language to automate certain tedious tasks and to perform actions that would represent a security risk on the client.
有许多服务器端解决方案:ASP、ASP.NET、ASP.NET MVC、Java、PHP、Ruby 等等。您需要了解的是,服务器端技术可以描述为特定服务器端语言的解析器,用于自动执行某些繁琐的任务并执行可能对客户端构成安全风险的操作。
Of course you will need to have the means to host such a site and to configure the hosting environment. For example, you could host a PHP website with Apacheand even have a developer hosting environment such as /MAMP/LAMP/WAMP.
当然,您需要拥有托管此类站点并配置托管环境的方法。例如,您可以使用Apache托管一个 PHP 网站,甚至可以拥有一个开发人员托管环境,例如 /MAMP/LAMP/WAMP。
You can view an example of including a page in PHP in the online documentation.
您可以在联机文档中查看在 PHP 中包含页面的示例。
Personally, I would be more inclined to use a server side solution.
就个人而言,我更倾向于使用服务器端解决方案。
回答by no.good.at.coding
HTML doesn't have an 'include' mechanism - I'm not sure where you've seen these solutions on StackOverflow. You've probably been looking at answers for a server side language such as PHP or ASP.
HTML 没有“包含”机制 - 我不确定您在 StackOverflow 上在哪里看到过这些解决方案。您可能一直在寻找诸如 PHP 或 ASP 之类的服务器端语言的答案。
You do have the following options for 'pure' HTML where you only have static pages and JavaScript:
对于只有静态页面和 JavaScript 的“纯”HTML,您确实有以下选项:
- Frames
- Ajax
- 框架
- 阿贾克斯
See my answer herefor sample code and more details (and also a caveat about SEO if that matters to you).
回答by Jordan
Make your html file you want to include to phpfile. (only change the extension - to .php
). If the files are in one directory, include code is:
制作您想要包含到php文件的html文件。(仅将扩展名 - 更改为.php
)。如果文件在一个目录中,则包含代码为:
<?php include "nameoffile.php" ?>