我在html文档中有一些ajax代码,可以将按钮的文本更改为存储在同一域中的另一个html文档中的文本。文本位于第二个html document.Like的body标记中:
<body> Text</body>因此,代码发出一个ajax请求,解析响应以创建一个xmldoc。当我尝试使用
getElementByTagName("body") or even getElementByTagName("html") 我得到了一个emepty HTMLcollection。但是当我使用
queryselector("body") I can get to the text. The log to console prints undefined. 下面是完整的代码:
function gettxt()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "http://localhost/ajax2.html", true);
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200)
{
allText = xmlhttp.responseText;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(allText, "application/xml");
var bodyobj=xmlDoc.getElementsByTagName("body");
console.log(bodyobj.lemgth);
document.getElementById("secbtn").value=bodyobj;
}
}
}
xmlhttp.send(null);
}我遗漏了什么?谢谢。
发布于 2012-04-18 02:44:32
我知道我做错了什么。我将按钮的文本设置为bodyobj而不是bodyobj.innerHTML。
https://stackoverflow.com/questions/10164218
复制相似问题