2015-09-06 77 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

虽然没有正确包含哪些行?如在“您没有在文本中包含文字上的线条”这一行上输入 –