2012-02-23 5 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

를 사용하지 않는 당신이이 확실히 작동합니다

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

이런 식으로 ... 내 머리 위로 떨어져

+1

+1

$('.eachLocation span.LocationAddress').each(function() { var address = $(this).html(); }); 
- 경우 다른 사람이 누락, Senad 필터 상황에 맞는 매개 변수를 사용 하였다. '$ ('selector', context)'. 알아두면 아주 유용합니다! – mrtsherman

+0

html() 대신 text()를 사용할 수 있습니까? 어떤 차이가 있습니까? –

+1

여기가 답을 찾을 수있는 곳입니다. http://api.jquery.com/text/ 기본적으로 text()는 모든 자식 요소에서 결합 된 텍스트 만 반환하고 html()은 html을 반환합니다. –

2

locationAddress는 각 위치의 형제가 아닙니다. 그래서 siblings

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

을 수행해야합니다 당신이 .html()를 사용하여 시도 되세요 .text() 대신? source은 또한 당신이 .children을 의미 생각하지 .siblings

1

HTML은 다음 변경하지 않을 경우 직접 주소를 타겟팅 할 수 있습니다 : -

는 범위가 일반 텍스트를 포함하는 경우

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

범위가 포함되어있는 경우 HTML

다른 방법으로
var address = $('.eachLocation div span.LocationAddress').html(); 

,

관련 문제