ドキュメント

Traversing/siblings

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Traversing

siblings[expr] )

Get a set of elements containing all of the unique siblings of each of the matched set of elements.
Can be filtered with an optional expression.
Arguments:
expr (Optional)String
An expression to filter the sibling Elements with
Examples:

Find the unique siblings of all yellow li elements in the 3 lists (including other yellow li elements if appropriate).

    var len = $(".hilite").siblings()
                          .css("color", "red")
                          .length;
    $("b").text(len);

<!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(){
    
    var len = $(".hilite").siblings()
                          .css("color", "red")
                          .length;
    $("b").text(len);

  });
  </script>
  <style>
  ul { float:left; margin:5px; font-size:16px; font-weight:bold; }
  p { color:blue; margin:10px 20px; font-size:16px; padding:5px; 
      font-weight:bolder; }
  .hilite { background:yellow; }
</style>
</head>
<body>
  <ul>
    <li>One</li>
    <li>Two</li>
    <li class="hilite">Three</li>
    <li>Four</li>
  </ul>
  <ul>
    <li>Five</li>
    <li>Six</li>
    <li>Seven</li>
  </ul>
  <ul>
    <li>Eight</li>
    <li class="hilite">Nine</li>
    <li>Ten</li>
    <li class="hilite">Eleven</li>
  </ul>
  <p>Unique siblings: <b></b></p>
</body>
</html>

Find all siblings with a class "selected" of each div.

$("p").siblings(".selected").css("background", "yellow");

<!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(){
    $("p").siblings(".selected").css("background", "yellow");
  });
  </script>
  
</head>
<body>
  <div><span>Hello</span></div>
  <p class="selected">Hello Again</p>
  <p>And Again</p>
</body>
</html>

NameType