0

JQueryで読み取ろうとしている次のxmlファイルがあります

<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 ループは何もしません。

私はしばらくの間、成功していない多くの例を見てきました。私はおそらく明らかな何かを見逃しています。ご協力いただきありがとうございます!

4

1 に答える 1

2

コードにエラーがあります

成功する

$(document).find("album").each(function(){  //this does not
     alert('1');  //never runs
     foo = $(this).find('image').text();
});
于 2013-04-27T19:32:38.190 に答える