Ho questo selettore jQuery:
"table tbody tr td:first-of-type"
Come posso costruire un selettore di celle per la stessa riga da $ (questo)? Ho bisogno di controllare
$(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
}
Domanda extra: dovrei evitare di usare selettori CSS3 come nth-child () e usare selettori jquery come eq ()?
risposte:
4 per risposta № 1Trova il più vicino tr
$(this).closest("tr")
E per selezionare a td
, uso .find()
incatenato a quello:
$(this).closest("tr").find("td") //and whatever criteria you need
0 per risposta № 2
Codice di lavoro:
<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>