后代元素选择器 (“ancestor descendant”)
descendant selector
描述:选择给定的祖先元素的所有的后代元素。
加入于: 1.0
jQuery( "ancestor descendant" )ancestor: 任何有效的选择器。
descendant: 一个用来筛选后代元素的选择器。
一个元素的后代元素必须是那个元素的子元素、孙代元素、重孙代元素,等等。
示例
标记所有的属于form元素的后代元素的input元素,并用蓝点边框框住它。给作为fieldsetr的后代元素的输入框一个黄色的背景,fieldset是form的后代元素。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>descendant demo</title>
<style>
form {
border: 2px green solid;
padding: 2px;
margin: 0;
background: #efe;
}
div {
color: red;
}
fieldset {
margin: 1px;
padding: 3px;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form>
<div>Form is surrounded by the green border.</div>
<label for="name">Child of form:</label>
<input name="name" id="name">
<fieldset>
<label for="newsletter">Grandchild of form, child of fieldset:</label>
<input name="newsletter" id="newsletter">
</fieldset>
</form>
Sibling to form: <input name="none">
<script>
$( "form input" ).css( "border", "2px dotted blue" );
$( "form fieldset input" ).css( "backgroundColor", "yellow" );
</script>
</body>
</html>
演示结果