在 html 中嵌入文本文件

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

Embed text files in html

htmltextembedtext-files

提问by damoiser

I would like to display the content of a text file inside a HTML page (.rtf, .txt, .log,...) stored on the server.

我想在存储在服务器上的HTML 页面 ( .rtf, .txt, .log,...) 中显示文本文件的内容。

I have tried with embedbut seems that doesn't work.

我试过,embed但似乎不起作用。

<embed src="/path_to_text_file/text.rtf" width="500" height="300">

There is a "simple" method (or tag) to do that or I should scan for the content and print it with, for example, jQuery?

有一个“简单”的方法(或标签)可以做到这一点,或者我应该扫描内容并使用例如 jQuery 打印它?

采纳答案by Keith V

Using a $.ajax() function with a .append() function inside you can easily grab the contents of a text document and display them on the page. Something along the lines of what you see below. Preform this on load of the page to immediately load the file in with the rest of the page.

使用带有 .append() 函数的 $.ajax() 函数,您可以轻松获取文本文档的内容并将它们显示在页面上。与您在下面看到的内容类似。在页面加载时执行此操作以立即将文件与页面的其余部分一起加载。

$.ajax({
        async:false,
        url: 'folder/file.txt',
        dataType: 'text',
        success: function(data) 
        {
        $('element').append(data);
            }
        });

Play around with it a little bit to get the correct result you are looking for. You could also use PHP but unless you really need to parse the data, PHP is a bit overkill in this situation.

稍微尝试一下以获得您正在寻找的正确结果。您也可以使用 PHP,但除非您确实需要解析数据,否则 PHP 在这种情况下有点过头了。

回答by JonasT

Something like this should do it:

像这样的事情应该这样做:

<object data="/path_to_text_file/text.txt" type="text/plain"
width="500" style="height: 300px">
<a href="/path_to_text_file/text.txt">No Support?</a>
</object>

回答by zzxx53

is only for plugin content (flash, etc). Try getting content using ajax, then write it with document.write; Or use the includetag in a back end language (PHP, ASP, etc)

仅适用于插件内容(Flash 等)。尝试使用 ajax 获取内容,然后使用document.write; 或者使用include后端语言(PHP、ASP 等)中的标签

回答by it Haffens

NOTE: apologies for the similarity to Keith V's answer and the fact he mentioned get requests - I only noticed his comment about get requests after posting my answer.

注意:对于与 Keith V 的回答的相似性以及他提到的 get requests 的事实深表歉意 - 我在发布我的回答后才注意到他关于 get requests 的评论。

I find the following structure helpful, and allows me to style the text as a span rather than having to wield an object:

我发现以下结构很有帮助,并允许我将文本设置为跨度而不是使用对象:

function append_url_content_to_div(url){
  $.get(url, function(returned_data){
    console.dir(returned_data);
    var content = '<span>'+returned_data+'</span>';
    $("#appendee_div").append(content);        
  });
}
append_url_content_to_div("https://dl.dropbox.com/s/euk874r7zd1cx0d/example_text.txt?dl=0"); //note that it has to be "dl." not "www." in dropbox
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="appendee_div"></div>

It works for me for dropbox content, but I don't see why it wouldn't work for publicly viewable text files. As you can see in the code, you need jquery.

它适用于 Dropbox 内容,但我不明白为什么它不适用于可公开查看的文本文件。正如您在代码中看到的,您需要 jquery。

回答by Dylan_Lockling

I found the best way to insert HTML5 code into a website is to save the code onto a new text document and embed that document using . Then you can format the code using css and divs.

我发现将 HTML5 代码插入网站的最佳方法是将代码保存到新的文本文档中并使用 . 然后你可以使用 css 和 div 来格式化代码。

回答by user3279149

Just use php. Change the html format to php and use

只需使用php。将html格式改成php并使用

echo file_get_contents("name_of_the_file.txt");

echo file_get_contents("name_of_the_file.txt");

Simple as that. You must use php because you have to output your text on the server side unless the info will not be shown to the client.

就那么简单。您必须使用 php,因为您必须在服务器端输出文本,除非信息不会显示给客户端。

I think is the easiest way to do what you want.

我认为这是做你想做的最简单的方法。