<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
div {
height: 300px;
width: 700px;
}
div p,
div h1,
div h2 {
float: left;
width: 100px;
height: 100px;
background-color: #ccc;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="box">
<p></p>
<p></p>
<h1 class="active">haha</h1>
<p></p>
<p class="active"></p>
<h2>hehe</h2>
</div>
<div class="box">
<p></p>
<p></p>
<h1 class="active">haha</h1>
<p></p>
<p class="active"></p>
<h2>hehe</h2>
</div>
<script src="./jquery-1.12.1.min.js"></script>
<script>
// 对每个box的第一个儿子设置背景色为红色 以下做法不正确
//$(".box").children().eq(0).css("background","red");//只是让第一个p的背景色发生变化
$('.box').each(function(){
$(this).children().eq(0).css('background-color','red');
})
</script>
</body>
</html>
网友评论