用循环将三个DIV变成红色
要点:
divarr=document.getElementById('out').getElementsByTagName('div');
返回数组divarr[],获得子元素数组
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#out div{
height: 100px;
width: 100px;
margin: 10px 10px;
background-color: #000000;
display: inline-block;
}
button{
margin: 10px auto;
}
</style>
</head>
<body>
<button type="button" onclick="fu()">点击将DIV变为红色</button>
<div id="out">
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
</div>
<script type="text/javascript">
var divarr=document.getElementById('out').getElementsByTagName('div'); //返回数组divarr[]
function fu(){
for(i in divarr){
divarr[i].style.backgroundColor='red';
}
}
</script>
</body>
</html>