我有这个jQuery选择器:
'table tbody tr td:first-of-type'
如何从$(this)构造同一行的单元格选择器?我需要检查
$(document).on('click','table tbody tr td:first-of-type',function()
{
if ( $('table tbody tr td:nth-child(2)').text() != "" ) // for the same row !!
//do something
else
//do something else
}
额外的问题:我应该避免使用像nth-child()这样的CSS3选择器并使用像eq()那样的jquery选择器吗?
答案 0 :(得分:5)
找到最近的tr
$(this).closest("tr")
要选择td
,请使用.find()
链接到:{/ p>
$(this).closest("tr").find("td") //and whatever criteria you need
答案 1 :(得分:0)
工作代码:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
if ($('table tbody tr td:nth-child(2)').text() != "" ){ // for the same row
//do something
// alert($('table tbody tr td:nth-child(2)').text());
}else{
//do something else
//alert("test");
}
});
</script>
</head>
<body>
<table>
<tbody>
<tr>
<td>itemOne</td>
<td>itemOneInfo</td>
<td>Info</td>
</tr>
</tbody>
</table>
</body>
</html>