next()関数は、一致する要素のセット内の直後の兄弟要素を取得するために使用されます。次の兄弟の要素だけが選択され、子要素は無視されます。

このnext()関数は ‘セレクタ’でフィルタリングすることができます。たとえば、next( ‘div’)は、<div>要素のみの直下の兄弟要素を取得するために使用されます。

jQuery next()の例

<html>
<head>
<style type="text/css">
  div,p {
    width:110px;
    height:40px;
    margin:2px 8px 64px 8px;
        float:left;
    border:1px blue solid;
  }
 </style>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>
  <h1>jQuery next() example</h1>

  <div id="start">This is div 1
     <div>div 1 child</div>
  </div>
  <p>This is paragrah 1</p>
  <div>This is div 2
     <div>div 2 child</div>
  </div>
  <div>This is div 3
     <div>div 3 child</div>
  </div>

  <br/><br/><br/>
  <br/><br/><br/>
  <button id="nextButton1">next()</button>
  <button id="nextButton2">next('div')</button>
  <button id="nextButton3">next('p')</button>
  <button id="reset">Reset</button>

<script type="text/javascript">

    var $currElement = $("#start");
    $currElement.css("background", "red");

    $("#nextButton1").click(function () {

      if(!$currElement.next().length){
        alert("No element found!");
        return false;
      }

      $currElement = $currElement.next();

      $("div,p").css("background", "");
      $currElement.css("background", "red");
    });

    $("#nextButton2").click(function () {

      if(!$currElement.next('div').length){
        alert("No element found!");
        return false;
      }

      $currElement = $currElement.next('div');

      $("div,p").css("background", "");
      $currElement.css("background", "red");
    });

    $("#nextButton3").click(function () {

      if(!$currElement.next('p').length){
        alert("No element found!");
        return false;
      }

      $currElement = $currElement.next('p');

      $("div,p").css("background", "");
      $currElement.css("background", "red");
    });

    $("#reset").click(function () {
      location.reload();
    });

</script>
</body>
</html>