0

このXMLファイルはここにあります。

<?xml version="1.0" encoding="UTF-8"?> 
<xml> 

<word> 
  <threeletter>RIP</threeletter>
  <threeletter>PIE</threeletter>  
  <fourletter>PIER</fourletter>
  <fourletter>RIPE</fourletter>
  <fiveletter>SPIRE</fiveletter> 
  <sixletter>SPIDER</sixletter> 
 </word>

 <word> 
  <threeletter>SUE</threeletter> 
  <threeletter>USE</threeletter> 
  <fourletter>EMUS</fourletter>
  <fourletter>MUSE</fourletter>
  <fiveletter>SERUM</fiveletter> 
  <sixletter>RESUME</sixletter> 
 </word>
</xml>

そして、ページの読み込みが完了したら、それらを読み込み、wordという配列にこれらの単語を格納します

$(document).ready(function() {

    $.ajax
    ({ 
        url: "dictionary.xml", 
        success: function( xml )
        { 
            $(xml).find("word").each(function()
            { 
            words.push($(this).text());
            }); 
        }       
    });

})

次に、その各コンテンツにアクセスすると、alert(word[0])この結果が表示されます

RIP
PIE  
PIER
RIPE
SPIRE 
SPIDER

つまり、word[0]は次のようなものだと思います。word[0] = "RIP PIE PIER RIPE SPIRE SPIDER "

しかし、私がこれをするとき」

var x = word[0].split(" ");
                    alert(x[0]);

「RIP」という言葉を私に与えていないのですが、なぜこれが起こっているのでしょうか。(xmlからの)すべての単語を分析し、words[0]それらの単語を分割して配列に格納したいのですが、なぜうまくいかないようです。

4

1 に答える 1

1

このようなものかもしれません

$.ajax({
    url: 'dictionary.xml',
    async: false,
    success: function(xml) {
        $(xml).find("word").each(function(index) {
            words[index] = [];
            $(this).children().each(function() {
                words[index].push($(this).text());

            });
        });

    },
    dataType: 'XML'
});
console.log(words[0]);
console.log(words[1]);​
于 2012-04-07T11:17:02.620 に答える