
- 0133技术站
- 联系QQ:18840023
- QQ交流群
- 微信公众号

jQuery children() 方法
定义和用法
children() 方法返回被选元素的所有直接子元素。
DOM 树:该方法只沿着 DOM 树向下遍历单一层级。如需向下遍历多个层级(返回子孙节点或其他后代),请使用 find() 方法。
提示:如需沿着 DOM 树向上遍历单一层级,或向上遍历直至文档根元素的所有路径(返回父节点或其他祖先),请使用parent() 或 parents() 方法。
注意:该方法不会返回文本节点。如需返回包含文本节点的所有子节点,请使用 contents() 方法。
语法
$(selector).children(filter)
| 参数 | 描述 |
| filter | 可选。规定缩小搜索子元素范围的选择器表达式。 |
<style>
.descendants *{
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("ul").children().css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body class="descendants">body (曾祖先节点)
<div style="width:500px;">div (祖先节点)
<ul>ul (直接父节点)
<li>li (子节点)
<span>span (孙节点)</span>
</li>
</ul>
</div>点击 "运行实例" 按钮查看在线实例
效果图:

推荐手册