當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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”)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。