JS에서 상위 노드 요소 및 하위 노드 요소 가져오기
6817 단어 HTML5+JS
/span>html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<table>
<tr>
<td>1td>
<td>2td>
<td>3td>
tr>
<tr>
<td id="t1">1td>
<td>2td>
<td>3td>
tr>
<tr>
<td>1td>
<td>2td>
<td>3td>
tr>
table>
body>
<script>
window.onload = function(){
// td
var t1 = document.getElementById('t1');
//
var tr = t1.parentNode;
tr.style.backgroundColor = 'yellow';
//
var t2 = tr.firstElementChild;
t2.innerText = '111';
var t3 = tr.lastElementChild;
t3.innerText = '333';
//
var t4 = t3.previousElementSibling;
t4.innerText = ' '
//
/*var t5 = t4.nextElementSibling;
t5.innerText = '12';*/
}
script>
html>