2015-09-06 104 views
0

我想輸出一個文本文件的行到HTA中的div。文本本身出現的很好,但是這些文字並沒有結束。相反,文本在一條大線上組合在一起。如果我打印到msgbox,它會顯示正確的分隔線。多行文本文件輸出html

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

在Windows中,換行符是'\ r \ n'。你應該替換它而不是'\ n'。 – Teemu

回答

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

你可以進一步說明我需要做些什麼來附上文本嗎? –

+0

默認情況下,HTML中的段落元素是分開的,因此您不需要在它們之間創建空行(添加非間斷空格),如果您想要,也可以是多餘的。 – fixmycode

+0

雖然沒有正確包含哪些行?如在「您沒有在文本中包含文字上的線條」這一行上輸入 –