我正在尝试获取相对于单击链接的表格单元格的文本值。单击“three”类的任何单元格中的链接,并获取该特定行的单元格的文本,命名为“one”。
<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());
但是如何获取该行中第一个单元格的文本?
我认为,作为一个例子,它会有点像:
console.log($(this).prev().prev().text());
但这不正确(返回“空字符串”)。有什么建议吗?
答案 0 :(得分:3)
尝试:
$(this).closest('td').siblings('.one').text();
答案 1 :(得分:1)
$("td.three a").click(function(e){
e.preventDefault();
var myText = $(this).closest("tr").find("td.one").text();
});
答案 2 :(得分:1)
这会有用吗?
$('td.three a').click(function(e) {
e.preventDefault();
var txt = $(this).prevAll('.one').text();
alert(txt);
});