JavaScript-부모자식 관계

hannah·2023년 8월 1일
0

JavaScript

목록 보기
50/121

parentNode와 children

현재 태그의 부모 태그를 찾고 싶을 때는 parentNode를 사용한다.

<table>
	<tr>
    	<td id='td00'></td>
        <td id='td01'></td>
        <td id='td02'></td>
    </tr>
</table>

위의 코드에서 tr 태그의 부모는 table 태그이다.

document.querySeletor('tr').parentNode;		//table 태그

반대로 자식 태그를 찾으려면 children 속성을 사용한다. 자식 태그는 여러 개일 수 있으므로 children 속성은 배열 모양의 값이 된다. 단, 진짜 배열은 아니고 배열 모양의 객체이다.

document.querySeletor('tr').children;		//{0:td, 1:td, 2:td}

0개의 댓글