2011-01-12 19 views
0

ここで2行目を使用する必要がありますか?jQueryを使用したXMLの解析

$("message",xml).each(function(id) { 
    message = $("message",xml).get(id); 
    msgID = $("msgID",message).text(); 

2番目の行を削除するための 'this'というキーワードはありませんか?

ありがとうございます!

答えて

3
$('message', xml).each(function() { 
    var msgID = $(this).find('msgID').text(); 
} 

このような構造を仮定:あなたはan .each()にいるときは、this繰り返し処理で現在の項目を表します

<root> 
    <message> 
     <msgID>123</msgID> 
    </message> 
    <message> 
     <msgID>234</msgID> 
    </message> 
    <message> 
     <msgID>345</msgID> 
    </message> 
</root> 
+0

jQueryシンボルとカッコを使用せずにthis.findを書くことができますか? –

+0

@Michaelいいえ、 'this'キーワードはXMLノードオブジェクト(または名前が何であれ)を参照するためですが、' find() 'はjQueryメソッドであり、jQueryオブジェクトに対してのみ呼び出すことができます。 –

+0

まあ、変数に '$(this)'を取り込むのは良い習慣です。 '$ $ this = $(this));' ... – Reigel

1

.each()には2つのパラメータもあります。 1番目は反復の現在のインデックス番号で、2番目は反復の項目で、thisと同じです。

$("message",xml).each(function(idx, val) { 
    // in here, "this" is the current "message" node in the iteration 
    // "i" is the current index in the iteration 
    // "val" is the same as "this" 
}); 

あなたは、あなたがxmlの上部にあるノードの下にをネストされている"message"ノードに対して$("message",xml)探しています。いずれかが見つかった場合は、.each()が繰り返し処理されます。