我有以下 xml 文件,我正在尝试使用 JQuery 读取
<albums startAlbumNo="1">
<album id="1">
<author>author1</author>
<image>images/2010-1-web-ready.jpg</image>
<caption>caption1</caption>
<tracks>
<item id="31">
<title>title1</title>
<artist>artist1</artist>
<song>content/stories/ChristmasStory.mp3</song>
</item>
<item id="32">
<title>title2</title>
<artist>artist2</artist>
<song>content/stories/ChristmasStory2.mp3</song>
</item>
</tracks>
</album>
//similiar albums below
<album>....</album>
<album>....</album>
<album>....</album>
</albums>
我有以下 JQuery 来阅读它
$(document).ready(function(){ //run when page loads
var buttonNames = new Array();
var foo;
function parse(document){ //extract song locations from mp3gallery.xml based upon album
buttonNames[0] = $(document).find('album[id="1"]').find("tracks").find("title").eq(0).text(); //this works
$(document).find("album").each(function(){ //this does not
alert('1'); //never runs
foo = $(this).find('image').text();
}
alert(foo); //never runs
changeButtonNames();
}
$.ajax({
url: 'mp3gallery/xml/mp3gallery.xml', // name of file you want to parse
dataType: "xml",
success: parse, //on success calls the "parse" function
error: function(){alert("Error: Something went wrong");}
});
function changeButtonNames(){ //has to be run last
document.getElementById('btn1').innerHTML = buttonNames[0];
}
});
这
$(document).find('album[id="1"]').find("tracks").find("title").eq(0).text();
运行良好,但是我的 .each 循环什么也不做。
一段时间以来,我一直在寻找许多示例,但没有成功。我可能遗漏了一些明显的东西。谢谢你的帮助!