2012-02-23 12 views
0

さまざまな場所とそのアドレスを保持するeachLocation Divのいくつかをループする必要があります。次のスパンjqueryを選択してください

$('.eachLocation').each(function(index) { 
     var address=$(this).siblings().find('span.LocationAddress').text(); 
    }); 

私はアドレスに値を取得していません???私は何か間違っているのですか?

<div class="eachLocation" > 
      <div class="LocationCounter">1.</div> 
      <div class="LocationInfo"> 
       <span class="LocationName">Kay Kay Center</span><br/> 
       <span class="LocationAddress"> 
        1019 fairfax road, Bellevue, NE 68005 
       </span><br/> 
      </div> 
     </div> 

答えて

2

を使用していません.text()の代わりに? sourceはまた、私はあなたが.childrenを意味するとは思いませ.siblings

+1

+1 - 他の人がそれを見逃している場合、セナードはcontextパラメータを使用してフィルタをかけました。 '$(セレクタ、文脈)'。それは知ることは非常に便利です! – mrtsherman

+0

html()の代わりにtext()を使用できますか?違いはありますか? –

+1

こちらがあなたの答えを探すことができます。 http://api.jquery.com/text/基本的にtext()はすべての子要素から結合されたテキストのみを返し、html()はhtmlを返します –

2

locationAddressは、各ロケーションの兄弟ではありません。あなたは.html()を使用してみました:だから、あなたはこのよう

$('.eachLocation').each(function(index) { 
    var address=$('span.LocationAddress', this).html(); 
}); 

が、これは私の頭の上から確認してください...

1

のために働くだろうそれを行う必要がありますsiblings

$('.eachLocation').each(function(index) {  
    var address=$(this).find('span.LocationAddress').text(); 
}); 
1

HTMLは、その後、変更されません場合は、直接アドレスをターゲットにすることができます: -

スパンは、通常のテキストに含まれている場合

var address = $('.eachLocation div span.LocationAddress').text(); 

スパンが含まれる場合、HTML

他の方法で
var address = $('.eachLocation div span.LocationAddress').html(); 

$('.eachLocation span.LocationAddress').each(function() { 
    var address = $(this).html(); 
}); 

関連する問題