简体   繁体   English

多行文本文件输出html

[英]Multiple Line textfile output html

I am trying to output the lines of a textfile to a div in an HTA. 我正在尝试将文本文件的行输出到HTA中的div。 The text itself comes up just fine, however the lines do not carry over. 文本本身显示的很好,但是行没有延续。 Instead, the text is grouped together on one big line. 而是将文本大行分组。 If I print to a msgbox it comes up with correct, separated, lines. 如果我打印到msgbox,则会显示正确且分开的行。

function updateTPtally()
{
    var fso, appdir, messagefile, ts, messagetext, line;
    fso = new ActiveXObject("Scripting.FileSystemObject");

    if (MESSAGE_FILE_NAME7.indexOf("\\") == -1) {
        appdir = unescape(fso.GetParentFolderName(location.pathname));
        messagefile = fso.BuildPath(appdir, MESSAGE_FILE_NAME7);
    } else {
        messagefile = MESSAGE_FILE_NAME7;
    }

    try {
        // Open the message-of-the-day file as a TextStream.
        ts = fso.OpenTextFile(messagefile, 1);
        messagetext = "";
        while (! ts.AtEndOfStream) {
            line = ts.ReadLine();
            // If the line contains text, enclose in <p> element;
            // otherwise, construct a blank line with a nonbreaking space.
            if (line.length > 0)
                line = line.replace(/^(.*)$/, "$1");
            else
                line = "<p>&nbsp;</p>";
            messagetext += line;
        }
        ts.Close();
    }

    // Construct an error message if an error occurred.
    catch(err) {
        messagetext = "<p>Can't display the message of the day.</p>"
        + "<p>&nbsp;</p>"
        + "<p>Error <b>0x" + hex(err.number) + "</b><br />"
        + err.description + "</p>";
    }

    // Update the innerHTML element of the textarea element with the
    document.getElementById("TPtallymemo").innerHTML = messagetext;
}

EDIT: I have added line = line.replace(/\\n/g, " 编辑:我添加了line = line.replace(/ \\ n / g,“
"); “);

This seems to work, however the first word of the text. 这似乎可行,但是该文本的第一个单词。 This is my textfile: 这是我的文本文件:

Hello.

This should be line two.  And written all in one line.

This should be line three, and also written on one line.

This is what prints out in my span: 这是我的跨度打印出来的:

Hello.


This
should be line two. And written all in one line.


This
should be line three, and also written on one line.

You are not enclosing the lines on the text after you replace them. 替换它们后,您无需将其括在文本中。 Also, you shouldn't enclose the non-breaking space, as paragraph elements account to be correctly separated from each other. 另外,您不应该封闭不间断空格,因为段落元素可以正确地彼此分隔。

Just one small final observation: you should call for AtEndOfStream() with the parentheses in your while condition. 只是一个小的最后观察结果:您应该在while条件中调用带有括号的 AtEndOfStream()。

messagetext = "";
while (! ts.AtEndOfStream()) {
    line = ts.ReadLine();
    // If the line contains text, enclose in <p> element;
    // otherwise, construct a blank line with a nonbreaking space.
    if (line.length > 0)
        line = line.replace(/^(.*)$/, "<p>$1</p>");
    messagetext += line;
}

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

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