DOM遍历可以分为以下三种情况:
- 查找父元素
- 查找子元素
- 查找兄弟元素
查找父元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
table{border-collapse: collapse;}
table,tr,td{border: 1px solid gray;}
</style>
<script type="text/javascript">
window.onload=function(){
var oTd=document.getElementsByTagName("td");
for(var i=0;i<oTd.length;i++){
oTd[i].onclick=function(){
var oParent=this.parentNode;//查找td的父元素
oParent.style.color="white";
oParent.style.backgroundColor="yellow";
};
}
}
</script>
</head>
<body>
<table>
<caption>考试成绩</caption>
<tr>
<td>小明</td>
<td>80</td>
<td>80</td>
<td&