2011-11-22 2 views
2

안녕하세요, 링크 바로 위에있는 스팬 요소에서 텍스트를 가져 오려고합니다.위의 스팬에서 텍스트 가져 오기 - Jquery

$(this).closest("span").text() 

전체 코드 :

JS

$boxes.find('.portlet-header .portlet-maximize').click(function() { 
// returns the rel of the link fine 
var widHtml = $(this).attr('rel'); 
// get text from span above link (not working) 
var widTitle = $(this).closest("span").text() 
}); 

HTML은

<div class="portlet-header"> 
<span class="widTitle gmIcon">text I want to get</span> 
<a rel="widgets/dashboard/max/goal-mouth" class="portlet-maximize"></a> 
</div> 

답변

4

closest() 조상 트리를 안내합니다. 의미가,

var widTitle = $(this).prev("span").text(); 
+0

감사하지만, 작동하지 않는 것 : 당신의 <span> 요소는 앵커의 형제가 아닌 조상이기 때문에, 대신 prev()를 사용해야합니다. 그러나 이것은 '$ (this) .siblings ('. widTitle '). 텍스트() – user1059511

+0

이 질문에 마크 업과 함께 작동합니다. 어쩌면 실제 마크 업과 다를 수 있습니까? –