PDA

View Full Version : گرفتن index ردیف در onclick



resident
سه شنبه 04 فروردین 1394, 15:25 عصر
سلام.
من یه جدول دارم تو html.
تو هر ردیف این جدول هم یه لینک دارم. میخوام روی onclick هر ردیف ، index ردیفش رو بگیرم.
با چه دستوری میشه index ردیف رو گرفت؟

CannibalCorpse
سه شنبه 04 فروردین 1394, 18:16 عصر
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border:1px solid black;
}
</style>
</head>
<body>


<p>Click on each tr element to alert its index position in the table:</p>


<table>
<tr onclick="myFunction(this)">
<td>Click to show rowIndex</td>
</tr>
<tr onclick="myFunction(this)">
<td>Click to show rowIndex</td>
</tr>
<tr onclick="myFunction(this)">
<td>Click to show rowIndex</td>
</tr>
</table>


<script>
function myFunction(x) {
alert("Row index is: " + x.rowIndex);
}
</script>


</body>
</html>