본문 바로가기

개발하자/자바스크립트

parentNode,previousSibling,nextSibling

<html>
<head>
<style type="text/css">
td {
border:1px solid navy;
width:50px; height:50px;
}
</style>
<script>
window.onload=function(){
tdTag.onclick=function(){
//this.parentNode.style.background="yellow";
//this.previousSibling.style.background="tomato";
//this.nextSibling.style.background="green";
}
trTag.onclick=function(){
this.childNodes[0].style.background="gray";
}
}
</script>
</head>
<body>
<table>
<tr id="trTag"><td>1</td><td id="tdTag">2</td><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td><td>7</td><td>8</td></tr>
</table>
</body>
</html>