当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


JQuery Descendant Selector (“ancestor descendant”)用法及代码示例

用法
descendant selector

说明:选择作为给定祖先的后代的所有元素。

  • 添加的版本:1.0jQuery( "ancestor descendant" )

    ancestor: 任何有效的选择器。

    descendant: 用于过滤后代元素的选择器。

元素的后代可以是该元素的子、孙、曾孙等。

例子:

使用蓝色虚线边框标记作为表单后代的所有输入。为作为表单后代的字段集的后代的输入提供黄色背景。

<!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-3.5.0.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>

演示:

相关用法


注:本文由纯净天空筛选整理自jquery.com大神的英文原创作品 Descendant Selector (“ancestor descendant”)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。