php 相当于 HTML 中的 include()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3928331/
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
Equivalent of include() in HTML
提问by Trufa
I was wondering wether there is a way to include some html content inside another html using only html?
我想知道是否有一种方法可以仅使用 html 在另一个 html 中包含一些 html 内容?
A replacement to PHP's
PHP 的替代品
<?php include("file.php"); ?>
Is this possible?
这可能吗?
EDIT:
编辑:
This has brought up some confusion, what I needed was "almost an html tag" that had the functionality of including a html document in another.
这引起了一些混乱,我需要的是“几乎是一个 html 标签”,它具有将 html 文档包含在另一个文档中的功能。
采纳答案by ?ime Vidas
It cannot be done purely by HTML. (There are iframes, however, but I don't think that qualifies in this case.)
它不能完全由 HTML 完成。(但是,有 iframe,但我认为在这种情况下不符合条件。)
It can be done using JavaScript. You get the other file via Ajax, and place its contents inside an HTML element on the current page.
它可以使用 JavaScript 来完成。您通过 Ajax 获取另一个文件,并将其内容放置在当前页面的 HTML 元素中。
回答by Victor
Have you tried:
你有没有尝试过:
<object type="text/html" data="file.html"></object>
回答by Michael Marr
Shameless plug of a library that I wrote the solve this.
我写的一个库的无耻插件解决了这个问题。
https://github.com/LexmarkWeb/csi.js
https://github.com/LexmarkWeb/csi.js
<div data-include="/path/to/include.html"></div>
The above will take the contents of /path/to/include.html
and replace the div
with it.
上面的内容将/path/to/include.html
取而代之div
。
回答by Mr Griever
HTML does not have a feature to include additional content natively. However most web servers do have server-side include statements:
SSI in Apache
SSI in IIS
HTML 本身没有包含附加内容的功能。但是,大多数 Web 服务器确实具有服务器端包含语句:IIS
中的 Apache
SSI 中的 SSI
回答by ITroubs
the only thing would be an iframe which is pure html. but you also can use javascript to get the page via ajax and include it into your dom hirarchy
唯一的就是一个纯 html 的 iframe。但您也可以使用 javascript 通过 ajax 获取页面并将其包含到您的 dom hirarchy 中
回答by Alex Howansky
If you're using Apache, you can try Server Side Includes
.
如果您使用的是 Apache,则可以尝试Server Side Includes
.
回答by Tyler Treat
There's no such thing. You'd have to use a server-side scripting language or JavaScript to do something like this.
没有这样的事情。您必须使用服务器端脚本语言或 JavaScript 来执行此类操作。
回答by prodigitalson
Yes there is but you need to enable it in your config or .htaccess:
是的,但您需要在配置或 .htaccess 中启用它:
Options +Includes
AddType text/html .shtml
AddHandler server-parsed .shtml
Of course with that youd need to rename any file doing the including to .shtml
... or you could jsut use:
当然,您需要将执行包括的任何文件重命名为.shtml
... 或者您可以使用:
Options +Includes
AddType text/html .html
AddHandler server-parsed .html
the syntax itself is similar to comment:
语法本身类似于注释:
<!--#include virtual="/footer.html" -->
回答by Cavemanharris
This might be a few years late but this is how i did it !
这可能晚了几年,但这就是我做到的!
in the first line after put this line !
在放这一行后的第一行!
<SCRIPT LANGUAGE="JavaScript" src="http://yourdomain.com/header.js">
then create a file called "header.js" and enter the content of the file you want to include ! like so....
然后创建一个名为“header.js”的文件并输入要包含的文件内容!像这样……
<!-- Begin
document.write('<center>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<a href="http://alinktosomewhere.co.za">a link 1</a>');
document.write('<hr>');
document.write('</center>');
// End -->
Hope this help !
希望这有帮助!
回答by Ray
Almost 10 years later, some people may still have doubts about this. So I'm going to explain a simple solution that we have today in 2020.
差不多10年过去了,可能还有人对此存有疑问。因此,我将解释我们今天在 2020 年拥有的一个简单解决方案。
I always use the jquery .load() function and never had a problem with that.
我总是使用 jquery .load() 函数并且从来没有遇到过这个问题。
Exemple: ( "#content" ).load( "includes/menu.html" );