使用 html5 在 html 中包含 html 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9001418/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:06:17  来源:igfitidea点击:

Include html file in html using html5

html

提问by lolo

I have 2 HTML files, suppose a.htmland b.html. In a.htmlI want to include b.html.

我有 2 个 HTML 文件,假设a.htmlb.html. 在a.html我想包括b.html.

In JSF I can do it like that:

在 JSF 中,我可以这样做:

<ui:include src="b.xhtml" />

It means that inside a.xhtmlfile, I can include b.xhtml.

这意味着在a.xhtml文件内部,我可以包含b.xhtml.

How can we do it in *.htmlfile?

我们如何在*.html文件中做到这一点?

using html 5 (if it can be done at all in html 5).

使用 html 5(如果它可以在 html 5 中完成)。

回答by greenoldman

Surprisingly, the same question was asked and it is possible: HTML5 include file

令人惊讶的是,同样的问题被问到了,而且是可能的:HTML5 包含文件

Rafa's answer:

拉法的回答:



Use the objecttag:

使用对象标签:

<object name="foo" type="text/html" data="foo.inc"></object>

foo.inc should include valid HTML.

foo.inc 应包含有效的 HTML。



I tested it on Konqueror, Firefox and Chromium.

我在 Konqueror、Firefox 和 Chromium 上对其进行了测试。

Note you must use a separate </object>tag otherwise any content after it gets discarded.

请注意,您必须使用单独的</object>标签,否则丢弃后的任何内容。

If you find it useful (I do), please upvote Rafa answer (not mine) because "it is not possible" is spreading like disease.

如果您觉得它有用(我愿意),请为 Rafa 的回答(不是我的)点赞,因为“这是不可能的”正在像疾病一样传播。

回答by Tor Valamo

If your server supports SSI (server side includes) you can put the following in your html-files without needing a scripting language or whatever. Apache has SSI enabled by default (I think?)

如果您的服务器支持 SSI(服务器端包括),您可以将以下内容放入您的 html 文件中,而无需脚本语言或其他任何东西。Apache 默认启用了 SSI(我认为?)

<!--#include file="same_path_file.html" -->
<!--#include virtual="docroot_file.html" -->

"file" is relative to the current file, and probably what you would use for including related files like "relevant_article_poll.html".

“文件”是相对于当前文件的,可能是您用于包含“relevant_article_poll.html”之类的相关文件的。

"virtual" is relative to document root (ie, your website root) and you would use it for including global files, like headers and footers.

“虚拟”相对于文档根目录(即您的网站根目录),您将使用它来包含全局文件,如页眉和页脚。

Doesn't really matter which one you choose, but it's useful to know the difference between the two.

您选择哪一个并不重要,但了解两者之间的区别很有用。

Also, the include directive makes a new internal http request to the server for each file, so you could include php files and the likes and they would be executed as they should.

此外,include 指令为每个文件向服务器发出一个新的内部 http 请求,因此您可以包含 php 文件等,并且它们将按应有的方式执行。

Here's a useful overview of SSI: http://en.wikipedia.org/wiki/Server_Side_Includes

以下是 SSI 的有用概述:http: //en.wikipedia.org/wiki/Server_Side_Includes

回答by Danny Verbiest

To use the PHP include function in HTML5, you just have to edit your .htaccess file as follows:

要在 HTML5 中使用 PHP 包含功能,您只需按如下方式编辑 .htaccess 文件:

    <Files *.html>
ForceType application/x-httpd-php
</Files>

Now you can use any PHP code in your html file like this:

现在您可以在您的 html 文件中使用任何 PHP 代码,如下所示:

<?php include 'menu.html'; ?>

Cheers ;)

干杯;)

回答by Ian Devlin

HTML5 is no different from HTML 4.01 in this sense in that this simply can't be done without scripting of some sort.

在这个意义上,HTML5 与 HTML 4.01 没有什么不同,因为如果没有某种脚本,这根本无法完成。