require_once 是否有 HTML 变体?
在HTML中是否有这样一行代码可以做与PHP的require_once相同的事情?我只是很好奇,因为我想在多张纸上复制一些代码行,而不需要要求自己在每页上键入它。
我知道我可以通过 PHP 做到这一点,但我正在寻找 HTML 变体?到底有没有这样的野兽,还是我找错了树?
In HTML is there such a line of code that will do the same thing as PHP's require_once? I'm just curious because there are some lines of codes that I want to duplicate through multiples sheets without needing to require myself to type it each page.
I know I can do it via PHP, but I am looking for an HTML variant? Is there such a beast or am I barking up the wrong tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这取决于您想要包含的内容。包含 PHP 文件是不可能的,如果你想包含 CSS 样式表,请使用:
对于 Javascript 文件,
当然你必须将该代码放在标题标签之间。
That depends on what you want to include. Including a PHP-File is not possible, if you want to include a CSS stylesheet, use:
and for a Javascript file
Of course you have to put that code between the header-tags.
不,HTML 中没有包含机制。除非你算SSI。
编辑:等等,“床单”?你是说CSS?
No, there is no include mechanism in HTML. Unless you count SSI.
Edit: wait, "sheets"? You mean CSS?
是的,SSI 是您能找到的最接近的。然而,有许多非服务器端方法可以解决这个问题。一些 Web 开发应用程序具有 html 模板系统,可以在开发端复制服务器端包含内容。例如,dreamweaver 允许您将可重复区域插入到 HTML 模板中。当您修改“包含”文件时,Dreamweaver 将修改使用该块的所有 HTML 文件。由于这不是真正的包含,而是一个 HTML 更新系统,因此如果您使用远程服务器,则必须重新上传这些文件,但如果您必须坚持使用纯 HTML,它可以使项目更易于管理和比使用 iframe 好得多。
最后,还可以选择让 Javascript 构建重复的代码块。您只需在每个页面上包含一个通用的 javascript 库
并让它在客户端(通过innerHTML 调用或将元素插入到DOM 中)。这有一个明显的缺点,
在服务器端语言中使用适当的包含当然是最好的方法,但在紧要关头这些都是潜在的替代方案。
Yeah, SSI is the closest you're going to get. However, there are many non-server-side ways to get around this. Several web development applications have html templating systems that replicate server-side includes on the development side. For example, dreamweaver allows you to insert repeatable regions into HTML templates. When you modify the "included" file, Dreamweaver will modify all HTML files that use that block. As this is not a true include, but rather an HTML-updating system, you do have to then re-upload these files if you use a remote server, but if you have to stick to plain HTML it can make projects much more manageable and is much better than using iframes.
Lastly, there is also the option of having Javascript build a repeating block of code. You can simply include a common javascript library on every page
<script type="text/javascript" src="templater.js"></script>
and have it build the element on the client's side (either with an innerHTML call or inserting elements into the DOM). This has the obvious downside thatUsing a proper include in a server side language is of course the best approach, but in a pinch these are both potential alternatives.
从技术上讲,您可以在页面上创建一个 iframe,它将加载和处理单独的页面,但它的功能不像 include 或 require 一次。据我所知,对此别无选择。
Technically you can create an iframe on your page which will load and handle a separate page but it does not function like include or require once. And to this I know of no alternatives.