children

« Обратно к списку traversing-функций

children[выражение] )

Получает набор элементов, содержащий всех непосредственных уникальных потомков для каждого совпавшего элемента.
Этот набор может быть дополнительно отфильтрован с помощью выражения (необязательно). Обратите внимание: тогда как parents() смотрит на всех «родителей», то children() рассматривает только непосредственные дочерние объекты.
Аргументы:

выражение (необязательно) Строка
Выражение, с помощью которого необходимо отфильтровать дочерние элементы.
Примеры:

Поиск всех потомков элемента по которому кликнул пользователь.

    $("#container").click(function (e) {
      $("*").removeClass("hilite");
      var $kids = $(e.target).children();
      var len = $kids.addClass("hilite").length;

      $("#results span:first").text(len);
      $("#results span:last").text(e.target.tagName);

      e.preventDefault();
      return false;
    });
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script>
  $(document).ready(function(){

    $("#container").click(function (e) {
      $("*").removeClass("hilite");
      var $kids = $(e.target).children();
      var len = $kids.addClass("hilite").length;

      $("#results span:first").text(len);
      $("#results span:last").text(e.target.tagName);

      e.preventDefault();
      return false;
    });

  });
  </script>

  <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 { margin:10px; border:1px solid transparent; }
  span { color:blue; border:1px solid transparent; }
  input { width:100px; }
  em { border:1px solid transparent; }
  a { border:1px solid transparent; }
  b { border:1px solid transparent; }
  button { border:1px solid transparent; }
  </style>
</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 href="#"><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>
</body>
</html>
Поиск всех потомков каждого элемента div.

$("div").children().css("border-bottom", "3px double red");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script>
  $(document).ready(function(){
    $("div").children().css("border-bottom", "3px double red");
  });
  </script>

  <style>
  body { font-size:16px; font-weight:bolder; }
  span { color:blue; }
  p { margin:5px 0; }
  </style>
</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>

</body>
</html>
Поиск всех потомков, обладающих классом «selected», каждого элемента div.

$("div").children(".selected").css("color", "blue");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script>
  $(document).ready(function(){
    $("div").children(".selected").css("color", "blue");
  });
  </script>
  <style>
  body { font-size:16px; font-weight:bolder; }
  p { margin:5px 0; }
  </style>

</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>
</body>
</html>
Name Type