相当于 HTML 中的 include()
我想知道是否有一种方法可以仅使用 html 将一些 html 内容包含在另一个 html 中?
PHP 的替代品
<?php include("file.php"); ?>
这可能吗?
编辑:
这引起了一些混乱,我需要的是“几乎是一个 html 标签”,它具有在另一个文档中包含 html 文档的功能。
I was wondering wether there is a way to include some html content inside another html using only html?
A replacement to PHP's
<?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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
你有没有尝试过:
Have you tried:
它不能纯粹通过 HTML 来完成。 (但是,有 iframe,但我认为这不符合这种情况。)
可以使用 JavaScript 来完成。您可以通过 Ajax 获取另一个文件,并将其内容放置在当前页面上的 HTML 元素内。
It cannot be done purely by HTML. (There are iframes, however, but I don't think that qualifies in this case.)
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.
我写的一个库的无耻插件解决了这个问题。
https://github.com/LexmarkWeb/csi.js
以上将采用
/path/ 的内容到/include.html
并用它替换div
。Shameless plug of a library that I wrote the solve this.
https://github.com/LexmarkWeb/csi.js
The above will take the contents of
/path/to/include.html
and replace thediv
with it.HTML 本身不具备包含附加内容的功能。然而,大多数 Web 服务器确实有服务器端包含语句:
Apache 中的 SSI
IIS 中的 SSI
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 的 iframe。但你也可以使用 javascript 通过 ajax 获取页面并将其包含到你的 dom 层次结构中
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
是的,但是您需要在配置或 .htaccess 中启用它:
当然,您需要将执行包含操作的任何文件重命名为
.shtml
...或者您可以使用:语法本身类似于评论:
Yes there is but you need to enable it in your config or .htaccess:
Of course with that youd need to rename any file doing the including to
.shtml
... or you could jsut use:the syntax itself is similar to comment:
没有这样的事。您必须使用服务器端脚本语言或 JavaScript 来执行此类操作。
There's no such thing. You'd have to use a server-side scripting language or JavaScript to do something like this.
如果您使用 Apache,可以尝试
服务器端包含
。If you're using Apache, you can try
Server Side Includes
.这可能晚了几年,但我就是这么做的!
在放置此行之后的第一行!
然后创建一个名为“header.js”的文件并输入您要包含的文件的内容!
像这样......
希望这有帮助!
This might be a few years late but this is how i did it !
in the first line after put this line !
then create a file called "header.js" and enter the content of the file you want to include !
like so....
Hope this help !
差不多10年过去了,有些人可能仍然对此存有疑问。因此,我将解释 2020 年我们今天拥有的一个简单解决方案。
我总是使用 jquery .load() 函数,并且从未遇到过问题。
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.
I always use the jquery .load() function and never had a problem with that.
Html 中缺少 Include\Import 确实令人沮丧!
一个好的替代方案是“服务器端包含 (SSI)”,以防“PHP”不受支持!
几乎所有(如果不是全部)Web 主机服务器都支持 SSI!
包含上述行的文件必须以“.shtml”或“.shtm”扩展名结尾!
浏览器本身无法执行像 Include\Import 这样简单的操作,这真的很烦人!
与 php 或 Node.js 一样,任何浏览器都应该支持在 HTML 加载过程开始之前使用 Javascript 本身对 html 进行预处理!
The lack of Include\Import in Html is really frustrating!
A good alternative is "Server Side Includes (SSI)" in case "PHP" is not supported!
SSI is supported by almost any (if not all) web host server!
The file that contains the above line must end with ".shtml" or ".shtm" extensions!
It's really annoying that something as easy as Include\Import can't be performed by the browser itself!
Like php or Node.js, preprocessing the html using Javascript itself before the HTML Load process start should be supported in any browser!
现在到了 2022 年,我使用 Vanilla Javascript fetch()。
这是如何包含
文件:“newfile.html”进入
div:“容器”
Now in 2022 I use Vanilla Javascript fetch().
This is how to include
file: "newfile.html" into
div: "container"