简体   繁体   English

txt文件数据转换为html文本

[英]Txt file data into html text

Newbie here on javascript and CSS, and I wanna know if it's possible to extract data from a .txt file and transform it to text and print on the page. 这里是javascript和CSS的新手,我想知道是否有可能从.txt文件中提取数据并将其转换为文本并在页面上打印。
I was searching through some old topics, and did find some similiar questions, and I tried to copy but didn't work hahha 我正在搜索一些旧主题,并且确实找到了一些类似的问题,因此我尝试复制但没有用,哈哈
Here it is what I did try: 这是我尝试的方法:

HTML: HTML:

<div id="printed"></div>

Script: 脚本:

var text_print = new XMLHttpRequest();
        text_print.open("GET", "folder/file.txt", true);
        text_print.send();
        text_print.onload = function (){
            console.log( text_print.responseText.slice(0, text_print.responseText.indexOf("\n")) ); };

    document.getElementById("printed").innerHTML=text_print;

The .txt file has only one line of text, btw. .txt文件只有一行文本,即btw。
Thank you. 谢谢。

The right answer depends on what you're trying to do. 正确的答案取决于您要执行的操作。

If you just want to to display the text (and that's it) 如果您只想显示文本(仅此而已)

you can use the embed tool 您可以使用嵌入工具

<embed src="test.txt">

Where 'test.txt' is the name of your text file. 其中“ test.txt”是文本文件的名称。

Note that you also need the path of the text file. 请注意,您还需要文本文件的路径。 So if test.txt is in the file directory below, you should use 因此,如果test.txt在下面的文件目录中,则应使用

<embed src="../test.txt">

or 要么

<embed src="folder/file.txt"

in your case. 在你的情况下。

This is very simple to type. 键入非常简单。 The disadvantage to this is that (as far as I'm aware) there is no way to apply styling to the text that gets displayed. 这样做的缺点是(据我所知)无法将样式应用于显示的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM