多行文本文件输出html



我试图将文本文件的行输出到HTA中的div。文本本身显示得很好,但是行不能结转。相反,文本被分组在一个大的行。如果我打印到消息框它会显示正确的分隔行。

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;
}

编辑:我添加了线=线。Replace (/n/g, "
");

这似乎有效,然而文本的第一个单词。这是我的文本文件:

Hello.
This should be line two.  And written all in one line.
This should be line three, and also written on one line.

这是在我的span中打印出来的内容:

Hello.

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

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

在替换文本后,您没有将文本上的行括起来。此外,你不应该把不换行的空格括起来,因为段落元素需要正确地彼此分开。

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

相关内容

  • 没有找到相关文章

最新更新