我有以下问题。
我的表格包含显示有关文章的简单信息的行。如果你点击一个标签,一个新的 <tr>
被插入。
在那<tr>
显示信息。但问题是,它的显示就像您在编辑器中查看 html 文档一样。
像这样:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-32" /> <title>Untitled Document</title> <link href="css/test2.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="search.js"> .......
不像通常那样“翻译”html。
我使用的代码:
function doDetail(articlenr)
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiceXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var partr = document.getElementById("tr"+articlenr);
var newtr = document.createElement("TR");
var newtd = document.createElement("TD");
var content = document.createTextNode(xmlhttp.responseText);
newtr.setAttribute('id', "det"+articlenr);
newtd.colSpan = 8;
newtd.appendChild(content);
newtr.appendChild(newtd);
partr.parentNode.insertBefore(newtr,partr.nextSibling);
}
}
xmlhttp.open("GET","showdetail.php?nr="+articlenr,true);
xmlhttp.send();
}
如何解决?
最佳答案
ajax responseText displayed as text, not html
是的,您明确地用这一行这样做:
var content = document.createTextNode(xmlhttp.responseText);
...然后将该行附加到 newtd
。
如果您希望将文本解析为 HTML,可以将其分配给元素的 innerHTML
属性。例如:
newtd.innerHTML = xmlhttp.responseText;
关于javascript - ajax responseText 显示为文本,而不是 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20823592/