多行文本文件输出html

时间:2015-09-06 05:42:17

标签: javascript html text-files hta

我正在尝试将文本文件的行输出到HTA中的div。文本本身很好,但线条不会延续。相反,文本在一个大线上组合在一起。如果我打印到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;
}

编辑: 我已经添加了   line = line.replace(/ \ n / g,&#34;
&#34;);

这似乎有效,但是文本的第一个字。这是我的文本文件:

Hello.

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

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

这是在我的范围内打印的内容:

Hello.


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


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

1 个答案:

答案 0 :(得分:0)

更换后,您没有在文本上包含这些行。此外,您不应该包含不间断的空格,因为段落元素可以正确地相互分离。

只是一个小的最终观察:你应该在你的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;
}