【问题标题】:Output content of a text file on a web page在网页上输出文本文件的内容
【发布时间】:2012-08-16 12:56:19
【问题描述】:

我有一个包含文档的 offline html 文件,我必须输出文本文件的内容(在我的例子中是源代码)。有没有办法仅使用客户端功能(HTML 本身或 JS)将此文件的内容包含到 html 页面?

索引.html

<html>
    <head>
    <style>
    p.source_code
    {
        font-family:"Courier New"
    }
    </style>
    </head>
    <body>
        <p>
            Some content
        </p>
        <p class="source_code">
            <!-- place for output content of file main.cpp -->
        </p>
    </body>
</html>

main.cpp

#include <stdio.h>
    int main()
    {
        printf("Hello World");
    }

我希望我的页面在浏览器中看起来像

    Some content

    #include <stdio.h>
    int main()
    {
        printf("Hello World");
    }

【问题讨论】:

  • 我不知道没有某种脚本可以做到这一点。
  • 这是不可能的。框架是一种可能性,但可能会产生令人讨厌的副作用。
  • @shambulator: 闪烁,滚动尴尬等,就像在窗外看页面一样,你可以尝试让它尽可能无缝,但这并不容易。
  • 很容易避开其中的一些&lt;iframe width=100% scrolling=no frameborder=no src="main.cpp" style="border: 0;" seamless&gt;

标签: html include


【解决方案1】:

如果 HTML 文件和源文件都是本地文件,您可以简单地使用 iframe

<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <iframe src="the-file.txt">
    </iframe>
  </body>
</html>

【讨论】:

  • 嗯嗯。根据我的回答:“如果 HTML 文件和源文件都是本地的”。它只会请求本地机器。 @jasonjifly 还是我错过了什么?
  • 可以,如果html文件是本地的,src可以设置为本地文件。
猜你喜欢
  • 2010-11-21
  • 1970-01-01
  • 2012-11-08
  • 2016-06-01
  • 1970-01-01
  • 2015-04-30
  • 2022-12-31
  • 2017-07-03
  • 1970-01-01
相关资源
最近更新 更多