我不得不修改这个问题,因为我遗漏了一段破坏事物的代码。我只想看看某个教会的图像。在jQuery中获取xml子节点的属性
XML文件:
<churches>
<church>
<data-name>germanevangelical</data-name>
<name>German Evangelical</name>
<address>501 Elm St.</address>
<opened>1887</opened>
<neighborhood>East</neighborhood>
<region>East</region>
<architecture>Gothic</architecture>
<denomination>Evangelical Lutheran</denomination>
<closed>2006</closed>
<image caption="Mary Smith">image_1_forweb.jpg</image>
<image caption="Mary Smith">image_2_forweb.jpg</image>
<image caption="Mary Smith">image_3_forweb.jpg</image>
</church>
... (more church nodes)
</churches>
我想用jQuery访问图像字幕。
这里是我的代码,但它返回“未定义”为标题:
var cName = 'germanevangelical';
$.ajax({
type: "GET",
url: "churchdata.xml",
dataType: "xml",
success: function(xml) {
var name = $(xml).find("data-name"); //get church names from xml file
$(name).each(function(id, item) {
if ($(item).text() == cName) { //find the right church in the xml file
$(item).parent().find("image").each(function(id, node) {
console.log('id: ' + $(node).attr('caption'));//undefined
})
}//end if right church in xml file
})
}
});
任何人都可以看到我做错了什么吗?
是'已经被解析为其中'dataType'设置为''xml'#document' xml'?在'.each()'' – guest271314
还缺少关闭')'好的,我是个白痴。我没有为我继续点击的教堂写的属性。我有上面教堂的属性。人为错误不容小觑...... – LauraNMS