jquery可点击表行,具有特定类名的td除外
我有一张这样的桌子jquery可点击表行,具有特定类名的td除外,jquery,tablerow,clickable,Jquery,Tablerow,Clickable,我有一张这样的桌子 <tbody> <tr class="row-links" data-link="http://mylink.com"> <td>some cell</td> <td>some cell</td> <td>some cell</td> <td class="special-td"><a href="http://fol
<tbody>
<tr class="row-links" data-link="http://mylink.com">
<td>some cell</td>
<td>some cell</td>
<td>some cell</td>
<td class="special-td"><a href="http://followthis.com">special cell</a></td>
<td>some cell</td>
<td class="special-td">special cell</td>
<td>some cell</td>
</tr>
</tbody>
$('.row-links').on('click', 'td:not(.special-td)', function(){
//get the link from data attribute
var the_link = $(this).parent().attr("data-link");
//do we have a valid link
if (the_link == '' || typeof the_link === 'undefined') {
//do nothing for now
}
else {
//open the page
window.location = the_link;
}
});
欢迎任何帮助
更新:
多亏了(PhoenixWing156&MDJ)提供的asnwers,工作代码现在看起来是这样的
<tbody>
<tr class="row-links" data-link="http://mylink.com">
<td>some cell</td>
<td>some cell</td>
<td>some cell</td>
<td class="special-td"><a href="http://followthis.com">special cell</a></td>
<td>some cell</td>
<td class="special-td">special cell</td>
<td>some cell</td>
</tr>
</tbody>
$('.row-links').on('click', 'td:not(.special-td)', function(){
//get the link from data attribute
var the_link = $(this).parent().attr("data-link");
//do we have a valid link
if (the_link == '' || typeof the_link === 'undefined') {
//do nothing for now
}
else {
//open the page
window.location = the_link;
}
});
数据链接属性是为“tr”元素定义的,而不是为单击的“td”元素定义的。您可能要更改此行:
//get the link from data attribute
var the_link = $(this).attr("data-link");
为此:
//get the link from data attribute
var the_link = $(this).parent().attr("data-link");
解决方案是同时使用Phoenix Wing156和MDJ答案。谢谢你们两位,不幸的是,我只能将一个答案标记为正确。谢谢你们的回答。我还必须使用PhoenixWing156代码来获取链接值