ドキュメント

Traversing/nextAll

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Traversing

nextAll[expr] )

Find all sibling elements after the current element.
Use an optional expression to filter the matched set.
Arguments:
expr (Optional)String
An expression to filter the next Elements with.
Examples:

Locate all the divs after the first and give them a class.

$("div:first").nextAll().addClass("after");

<!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:first").nextAll().addClass("after");
  });
  </script>
  <style>
  div { width: 80px; height: 80px; background: #abc; 
        border: 2px solid black; margin: 10px; float: left; }
  div.after { border-color: red; }
  </style>
</head>
<body>
  <div>first</div>
  <div>sibling<div>child</div></div>
  <div>sibling</div>
  <div>sibling</div>
</body>
</html>

Locate all the paragraphs after the second child in the body and give them a class.

    $(":nth-child(1)").nextAll("p").addClass("after");

<!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(){
    
    $(":nth-child(1)").nextAll("p").addClass("after");

  });
  </script>
  <style>
  div, p { width: 60px; height: 60px; background: #abc;
           border: 2px solid black; margin: 10px; float: left; }
  .after { border-color: red; }
  </style>
</head>
<body>
  <p>p</p>
  <div>div</div>
  <p>p</p>
  <p>p</p>
  <div>div</div>
  <p>p</p>
  <div>div</div>
</body>
</html>

NameType