簡體   English   中英

多行文本文件輸出html

[英]Multiple Line textfile output html

我正在嘗試將文本文件的行輸出到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,“
“);

這似乎可行,但是該文本的第一個單詞。 這是我的文本文件:

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.

替換它們后,您無需將其括在文本中。 另外,您不應該封閉不間斷空格,因為段落元素可以正確地彼此分隔。

只是一個小的最后觀察結果:您應該在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