728x90
$(this).parent().parent().index();
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$("button").on("click", function() {
var index = $(this).parent().parent().index();
console.log("index : "+ index);
$("#test").val(index); // test를 위한 input에 값넣기
});
});
</script>
</head>
<body>
<div class="container">
<input id="test" type="text" class="inputbox" size="10" />
<table class="table">
<thead>
<tr>
<th>aaaaa</th>
<th>aaaaa</th>
<th>aaaaa</th>
<th>aaaaa</th>
<th>aaaaa</th>
</tr>
</thead>
<tbody>
<tr>
<td>aaaaa</td>
<td>bbbbb</td>
<td>aaaaa</td>
<td>bbbbb</td>
<td><button type="button">click!!</button></td>
<tr>
<td>aaaaa</td>
<td>bbbbb</td>
<td>aaaaa</td>
<td>bbbbb</td>
<td><button type="button">click!!</button></td>
<tr>
<td>aaaaa</td>
<td>bbbbb</td>
<td>aaaaa</td>
<td>bbbbb</td>
<td><button type="button">click!!</button></td>
</tr>
<tr>
<td>aaaaa</td>
<td>bbbbb</td>
<td>aaaaa</td>
<td>bbbbb</td>
<td><button type="button">click!!</button></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
$(this).parent(); - td
$(this).parent().parent(); - tr
$(this).parent().parent().index(); - tr의 인덱스 값
'js' 카테고리의 다른 글
[JavaScript] addEventListener() 이벤트 등록 (0) | 2021.07.26 |
---|---|
[JavaScript] Date 날짜 간단 표현 / toISOString() (0) | 2021.06.03 |
[jQuery] textarea 글자 수 count / keyup event (1) | 2021.05.17 |
[javaScript] 특정한 데이터를 DOM 요소에 저장 / dataset (4) | 2021.05.13 |
[javaScript] 페이지 내에 원하는 위치로 스크롤 이동하기 (1) | 2021.05.12 |