---
title: "jquery遍历-后代遍历"
author: "小丑鱼0703"
date: "2015-01-20T18:20:58Z"
tags: []
categories: ["Web前端"]
slug: "jquery"
draft: false
lastmod: "2015-01-20T18:20:58Z"
id: 2177769
url: "https://wangjingyi.iteye.com/blog/2177769"
---

children() 方法返回被选元素的所有直接子元素。
find() 方法返回被选元素的后代元素，一路向下直到最后一个后代。



```html
<style>
.descendants *
{ 
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script>
$(document).ready(function(){ 
   $("div").find("*").css({"color":"red","border":"2px solid red"});
   $("div").children().css({"color":"yellow","border":"2px solid yellow"});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div (当前元素) 
  <p>p (子)
    <span>span (孙)</span>     
  </p>
  <p>p (child)
    <span>span (孙)</span>
  </p> 
</div>
</body>
```

![](http://dl2.iteye.com/upload/attachment/0105/4405/af4330ef-e4f8-31d8-aaf7-313eb5eb9747.png)