我想点击表格中的锚标记(href)并提取下一个单元格(或该行中任何特定单元格)的内容
$(".clCode").click(function(){
alert( $(this).text() );
return false;
});
<table>
<tr>
<td class='clCode'><a href="#">Select</a></td><td>Code123</td>
</tr><tr>
<td class='clCode'><a href="#">Select</a</td><td>Code543</td>
</tr><tr>
<td class='clCode'><a href="#">Select</a</td><td>Code987</td>
</tr>
</table>
答案 0 :(得分:3)
$(".clCode").click(function(){
alert( $(this).parent().next().text() );
return false;
});
那应该得到下一个td。如果有更多的tds,你也可以将一个选择器传递给next(),你想得到的不是第一个。
$(".clCode").click(function(){
alert( $(this).parent().next(':last').text() );
return false;
});
答案 1 :(得分:1)
从下一个单元格中获取文本非常简单:
$("td.clCode a").click(function(e){
e.preventDefault();
var nextText = $(this).parent().next().text();
});
从另一个单元格中获取文本可以通过其在周围表格行中的索引来完成:
$("td.clCode a").click(function(e){
e.preventDefault();
// Get text from fourth table-cell in row.
var cellText = $(this).closest("tr").find("td:eq(3)").text();
});
答案 2 :(得分:0)
$("td.clCode a").click(function() {
var code = $(this).parent().next().text();
alert(code);
return false;
});
答案 3 :(得分:0)
$(".clCode a").click(function() {
alert($(this).parent().next().html();
return false; // optional
});
我认为您的HTML标记有点多余。
答案 4 :(得分:0)
您可以使用jquery next()函数获取兄弟姐妹列表:
$(".clCode").click(function(){
tmp = $(this).next()[0];
alert($(tmp).text());
});