将文本文档导入 HTML

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

Importing a text document into HTML

html

提问by maasha

I was wondering if there is a HTML element that allows you to incorporate a text file into a HTML document - something ala <pre src="test.txt"\>?

我想知道是否有一个 HTML 元素可以让您将文本文件合并到一个 HTML 文档中 - ala <pre src="test.txt"\>

回答by Kuuchuu

I realize the question is old, but this is for reference for people that later come to this question. I often use the embed tag. It was introduced in HTML5, and isn't ugly like iframes. You will still need to specify width and height, but I suggest doing so with css.

我意识到这个问题很老,但这是供后来提出这个问题的人参考。我经常使用 embed 标签。它是在 HTML5 中引入的,不像 iframe 那样难看。您仍然需要指定宽度和高度,但我建议使用 css 来指定。

<embed src="test.txt">

Notice, the embed tag is open ended. The embed tag is supported in all major browsers.

请注意,嵌入标签是开放式的。所有主要浏览器都支持 embed 标签。

回答by Alexander Molodih

Most similar is iframe

最相似的是 iframe

<iframe src="test.txt" width="100%" height="300">
</iframe>

P/S: Most flexible solution is using JavaScript (How to read from file)

P/S:最灵活的解决方案是使用 JavaScript(如何从文件中读取

回答by oezi

as you said, another file is another document, so you'll have to use something that allows you to display inline documents: the <iframe>(see wikipediafor more info)

正如您所说,另一个文件是另一个文档,因此您必须使用允许您显示内联文档的内容:(有关更多信息,<iframe>请参阅维基百科

<iframe src="test.txt" height="200" width="200">
  Your Browser doesn't support Frames.
</iframe>

if this isn't what you want (frames are... ugly), the other ways to do semething like this is a server-side includeor load the document into any element with javascript/ajax (with jQuery, for example).

如果这不是你想要的(框架是......丑陋的),其他的方法是使用 javascript/ajax(例如,使用 jQuery)在服务器端包含或将文档加载到任何元素中。

回答by Chris J

Not standard HTML.

不是标准的 HTML。

You might thought want to look to see if your server supports server-side includesalthough these are often disabled for being a percieved security risk.

您可能想查看您的服务器是否支持服务器端包含,尽管这些通常会因为感知到的安全风险而被禁用。

You'd then be able to do:

然后你就可以做到:

<pre>
<!--#include virtual="text.txt" -->
</pre>

In order to get them parsed properly by the server, you may have to give the HTML file a different extension (e.g., .shtml, .shtm) -- this is also specific to a given server's configuration.

为了让服务器正确解析它们,您可能必须给 HTML 文件一个不同的扩展名(例如,.shtml、.shtm)——这也特定于给定服务器的配置。

Otherwise you'll have to have some dynamic script (CGI, ASP, ASPX, PHP, whatever) to merge the two together serverside.

否则,您将不得不使用一些动态脚本(CGI、ASP、ASPX、PHP 等)将两者合并到服务器端。