2010-02-10 3 views
1

클릭 한 링크를 기준으로 표 셀의 텍스트 값을 가져 오려고합니다. 클래스의 모든 세포에있는 링크를 클릭하여 ''세와 특정 행의 세포라는 이름의 클래스의 텍스트 가져 오기 '를'jQuery - 클릭 된 요소와 관련된 값을 찾습니다.

<tr> 
<td class='one'><a href="#">Text to get</a></td> 
<td class='two'>meh</td> 
<td class='three'><a href="#">Click this to get the text in the first cell of this row</a></td> 
</tr> 
<tr> 
<td class='one'>Different text to get</td> 
<td class='two'>blah</td> 
<td class='three'><a href="#">Click this to get the text in the first cell of this row</a></td> 
</tr> 

내가 좋아하는 뭔가를 클릭 한 요소의 텍스트를 얻을 수 있습니다.

console.log($(this).text()); 

는하지만 내가 어떻게 행의 첫 번째 셀의 텍스트를받을 수 있나요 내가 예를 들어, 그것은 somethng 등이 될 것이라고 생각

:

console.log($(this).prev().prev().text()); 

하지만 올바르지 않습니다 ("빈 문자열"반환). 어떤 제안?

답변

3

시도 :

$(this).closest('td').siblings('.one').text();

+0

빵 ... 인스턴트 솔루션입니다. CalebD에게 감사드립니다. – Stuart

1
$("td.three a").click(function(e){ 
    e.preventDefault(); 
    var myText = $(this).closest("tr").find("td.one").text(); 
}); 
1

것이 작품?

http://jsbin.com/opafa

$('td.three a').click(function(e) { 
    e.preventDefault(); 

    var txt = $(this).prevAll('.one').text(); 

    alert(txt); 
}); 
관련 문제