Javascript 多行文本文件输出html
我试图将文本文件的行输出到HTA中的div。这篇文章本身写得很好,但行没有延续下去。相反,文本分组在一条大线上。如果我打印到msgbox,它会显示正确的分隔行Javascript 多行文本文件输出html,javascript,html,text-files,hta,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
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> </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> </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;
}
这是在我的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;
}
messagetext=”“;
而(!ts.AtEndOfStream()){
line=ts.ReadLine();
//如果该行包含文本,请将其括在元素中;
//否则,请使用不间断空格构造一个空行。
如果(直线长度>0)
行=行。替换(/^(.*)$/,“$1”;
messagetext+=行;
}
您能进一步说明我需要做些什么来封装文本吗?HTML中的段落元素默认是分开的,因此您不需要在它们之间创建空行(添加不间断的空格),如果您愿意,可以这样做,但这是多余的。但哪些行没有正确封装?正如这一行中的“替换后,文本中的行不会被括起来:line=line.replace(/^(.*)$/,“$1”)
您忘记将匹配组$1括在
和
之间以创建段落。在Windows中,新行字符为\r\n
。您应该替换它,而不仅仅是\n
。
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;
}