从另一个调用一个 html 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2809652/
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
Call one html file from another
提问by Ankita
I want to call one html page fron another in a div.
我想在 div 中从另一个页面调用一个 html 页面。
I tried using
我尝试使用
<include file="NavigationTree.html" />
and
和
<? include("/starfix/pages/NavigationTree.html"); ?>
But it doesn't work. Am I doing something wrong or do i need to do it some other way?
但它不起作用。我做错了什么还是我需要以其他方式做?
回答by Daniel Vassallo
You may want to consider using Server Side Includes(SSI).
您可能需要考虑使用服务器端包含(SSI)。
You would place your HTML snippet into a separate file, such as NavigationTree.html
, and then you would simply reference it in your web pages by using:
您可以将您的 HTML 片段放入一个单独的文件中,例如NavigationTree.html
,然后您只需使用以下命令在您的网页中引用它:
<!--#include virtual="NavigationTree.html" -->
SSI is supported by all the popular web servers, including Apache, IIS and lighttpd.
所有流行的 Web 服务器都支持 SSI,包括 Apache、IIS 和 lighttpd。
Note that if you are using a shared host, you may have to use the .shtml
, .stm
, or .shtm
extension for SSI to work. If you have root access to your web server, it can be easily configured to enable SSI for any extension, including html
.
请注意,如果您使用的是共享主机,您可能需要使用的.shtml
,.stm
或.shtm
扩展SSI工作。如果您对 Web 服务器具有 root 访问权限,则可以轻松配置它为任何扩展启用 SSI,包括html
.
回答by Pekka
This is not possible in pure HTML.
这在纯 HTML 中是不可能的。
The former is a notation I have never seen before, it is not HTML, maybe it works in some specific server-side templating language.
前者是我以前从未见过的符号,它不是 HTML,也许它适用于某些特定的服务器端模板语言。
The latter is PHP. It should work but you need to bear in mind include()
works with absolutepaths inside the server's file system.
后者是PHP。它应该可以工作,但您需要记住include()
使用服务器文件系统内的绝对路径。
You should specify a relative path:
您应该指定一个相对路径:
<? include("./NavigationTree.html"); // will work if it's in the same directory ?>
or an absolute one that will probably look something like this:
或者绝对的,可能看起来像这样:
<? include("/path/to/your/www/dir/starfix/pages/NavigationTree.html"); ?>
(ask your admin for the absolute path to your web root)
(向您的管理员询问您的 Web 根目录的绝对路径)
You can maybe also do a HTTP include:
您也许还可以执行 HTTP 包括:
but that's unwise because it tends to be slow, and generates a second request on each page request.
但这是不明智的,因为它往往很慢,并且在每个页面请求上生成第二个请求。
You can also use SSI as outlined by @Daniel.
您还可以使用@Daniel 概述的 SSI。
回答by Garry_G
You could also use jQuery for this,
您也可以为此使用 jQuery,
e.g.
例如
<div id="yourDiv" />
<script>
$("#yourDiv").load("NameOfYourPageToReadFrom.ext #NameOfDivToReadFrom");
</script>
This puts the contents of the 'NameOfDivToReadFrom' DIV in the called file ''NameOfYourPageToReadFrom' into the loaded DIV ('yourDiv') in your current file.
这会将被调用文件“NameOfYourPageToReadFrom”中的“NameOfDivToReadFrom” DIV 的内容放入当前文件中加载的 DIV(“yourDiv”)中。
Remember to add the definition to the header part of your html.
请记住将定义添加到 html 的标题部分。
e.g.
例如
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
回答by ЯegDwight
You can use an iframe
for that, e.g.:
您可以iframe
为此使用一个,例如:
<iframe width="500" height="300" frameborder="no" scrolling="no" marginheight="0" marginwidth="0"
src="http://www.example.com/page.html"></iframe>