.children()
返回: jQuery
.children( [selector ] )
描述:获得匹配的元素集合中每个元素的子元素,视情况用一个选择器作筛选。
给定一个jQuery对象,它代表了一个DOM元素集合,.children()方法允许我们在DOM树中搜索遍这些元素的子元素,并从匹配的元素构造一个新的jQuery对象。.children()方法不同于.find()方法之处是,.children()方法只遍历DOM树中的一级,与此同时.find()可以遍历多级,选中后代元素(孙代元素,等等)。注意,和大多数jQuery方法那样,.children()并不返回文本节点;要想获得所有的子元素,包括文本节点和注释节点,请使用.contents()方法。
.children()方法视情况接受一个选择器表达品茶,类型与传给$()函数的相同。如果提供了该选择器,将筛选子元素,测试它们是否匹配该选择器。
设想一个网页,上面有一个基本嵌套的列表:
<ul class="level-1">
<li class="item-i">I</li>
<li class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>
如果我们从第二层列表开始,我们可以找到它的子元素:
$( "ul.level-2" ).children().css( "background-color", "red" );
这个调用的结果是在项目A、B、C后面有一个红色的背景。因为我们并不提供一个选择器表达式,所有的子元素都是返回的晚对象的一部分。如果我们提供了一个选择器表达式,三个项目中只有匹配的项目包含在返回的jQuery对象中。
示例
找到被点击元素所有的子元素。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>children demo</title>
<style>
body {
font-size: 16px;
font-weight: bolder;
}
div {
width: 130px;
height: 82px;
margin: 10px;
float: left;
border: 1px solid blue;
padding: 4px;
}
#container {
width: auto;
height: 105px;
margin: 0;
float: none;
border: none;
}
.hilite {
border-color: red;
}
#results {
display: block;
color: red;
}
p, span, em, a, b, button {
border: 1px solid transparent;
}
p {
margin: 10px;
}
span {
color: blue;
}
input {
width: 100px;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="container">
<div>
<p>This <span>is the <em>way</em> we</span>
write <em>the</em> demo,</p>
</div>
<div>
<a ><b>w</b>rit<b>e</b></a> the <span>demo,</span> <button>write
the</button> demo,
</div>
<div>
This <span>the way we <em>write</em> the <em>demo</em> so</span>
<input type="text" value="early"> in
</div>
<p>
<span>t</span>he <span>m</span>orning.
<span id="results">Found <span>0</span> children in <span>TAG</span>.</span>
</p>
</div>
<script>
$( "#container" ).click(function ( event ) {
$( "*" ).removeClass( "hilite" );
var kids = $( event.target ).children();
var len = kids.addClass( "hilite" ).length;
$( "#results span:first" ).text( len );
$( "#results span:last" ).text( event.target.tagName );
event.preventDefault();
});
</script>
</body>
</html>
演示结果
找到每个div中所有的子元素。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>children demo</title>
<style>
body {
font-size: 16px;
font-weight: bolder;
}
span {
color: blue;
}
p {
margin: 5px 0;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>Hello (this is a paragraph)</p>
<div><span>Hello Again (this span is a child of the a div)</span></div>
<p>And <span>Again</span> (in another paragraph)</p>
<div>And One Last <span>Time</span> (most text directly in a div)</div>
<script>
$( "div" ).children().css( "border-bottom", "3px double red" );
</script>
</body>
</html>
演示结果
找到每个div中所有的带有“selected”样式类的子元素。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>children demo</title>
<style>
body {
font-size: 16px;
font-weight: bolder;
}
p {
margin: 5px 0;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div>
<span>Hello</span>
<p class="selected">Hello Again</p>
<div class="selected">And Again</div>
<p>And One Last Time</p>
</div>
<script>
$( "div" ).children( ".selected" ).css( "color", "blue" );
</script>
</body>
</html>
演示结果