ボタンのテキストを同じドメイン内の別のhtmlドキュメントに保存されているテキストに変更するajaxコードがhtmlドキュメントにあります。テキストは2番目のhtmlドキュメントのbodyタグにあります。次のようになります。
<body> Text</body>
そのため、コードはajaxリクエストを作成し、レスポンスを解析してxmldocを作成します。私が使おうとすると
getElementByTagName("body") or even getElementByTagName("html")
emeptyHTMLcollectionを取得します。しかし、私が使用するとき
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);
}
私は何が欠けていますか?ありがとう。