以下代码返回我的 ID。
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "https://brewslocal.com/brewery-images-xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("photoBox").innerHTML = xmlDoc.getElementsByTagName("image")[0].id;
}
<div id="photoBox"></div>
当我更改为下一个属性 imageurl 时,它返回未定义。
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "https://brewslocal.com/brewery-images-xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("photoBox").innerHTML = xmlDoc.getElementsByTagName("image")[0].imageurl;
}
<div id="photoBox"></div>
有什么想法吗?
最佳答案
您可以使用 Element.getAttribute('attributeName')
从此自定义 HTML 元素获取属性值。
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("photoBox").innerHTML = xmlDoc.getElementsByTagName("image")[0].getAttribute('imageurl');
}
关于Javascript XML 对象 - 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120063/