我目前正在尝试从文件中读取文本,并将其附加到使用DOM和Javascript的HTML页面中的元素。我不能让文本格式化。我尝试过使用innerHtml,但根本没有格式化(没有换行符)。使用Javascript插入文件到html页面?
这里是JavaScript:
http = new XMLHttpRequest();
http.open("GET",FILE,false);
http.send();
document.getElementById("tbody").innerHTML = http.responseText
就像我说的文本被添加到tbody元素,但其格式都没有。
我得到了它与此代码(与预先标记)的工作,但就像我说的它的作品在我的电脑但不这不利于在服务器上。
http.open("GET",FILE ,false);
http.send();
var newtext = document.createTextNode(http.responseText);
var para = document.getElementById("tbody");
para.appendChild(newtext);
这是我的所有JavaScript代码:
功能getHTTPObject() { 变种HTTP = FALSE;
/*@cc_on
@if (@_jscript_version >= 5)
try
{
http = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
http = false;
}
}
@else
{
http = false;
}
@end @*/
if (!http && typeof XMLHttpRequest != 'undefined')
{
try
{
http = new XMLHttpRequest();
}
catch (e)
{
http = false;
}
}
return http
}
function loadData()
{
http = getHTTPObject();
if (http)
{
http.open("GET","my file name",false);
http.send();
var newtext = document.createTextNode(http.responseText);
var para = document.getElementById("tbody");
para.appendChild(newtext);
}
}
您需要提供更多详细信息供人进一步帮助。答案的内容究竟是什么?你开始使用的html是什么样的?当您收到请求时,DOM的状态是什么? – 2008-12-08 00:04:38