weixin_33714884 2017-05-08 20:04 采纳率: 0%
浏览 105

txt文件数据转换为html文本

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.
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:

<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("
")) ); };

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

The .txt file has only one line of text, btw.
Thank you.

  • 写回答

1条回答 默认 最新

  • weixin_33682790 2017-05-09 02:39
    关注

    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.

    Note that you also need the path of the text file. So if test.txt is in the file directory below, you should use

    <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.

    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭