0

我在一个 html 文档中有一些 ajax 代码,它将按钮的文本更改为存储在同一域中另一个 html 文档中的文本。文本在第二个 html 文档的 body 标记中。像这样:

<body> Text</body>

因此代码发出 ajax 请求,解析响应以创建 xmldoc。当我尝试使用

getElementByTagName("body") or even getElementByTagName("html") 

我得到一个空的 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);

}

我错过了什么?谢谢。

4

1 回答 1

0

我知道我做错了什么。我将按钮的文本设置为 bodyobj 而不是 bodyobj[0].innerHTML。

于 2012-04-17T18:44:31.640 回答