我不得不修改這個問題,因爲我遺漏了一段破壞事物的代碼。我只想看看某個教會的圖像。在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