jQuery next() 方法


jQuery 中的 next() 選擇器用於返回所選元素的下一個同級元素。

語法

語法如下 −

$(selector).next(filter)

上述引數 filter 是一個選擇器表示式,用於縮小下一個同級元素的搜尋範圍

示例

讓我們看一個實現 jQuery next() 選擇器的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   .demo * {
      display: block;
      border: 3px dashed red;
      padding: 3px;
      margin: 25px;
   }
   .one {
      border: 2px solid yellow;
   }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("span.test").next().addClass("one");
   });
</script>
</head>
<body>
<h1>Heading One</h1>
<div class="demo" style="width:600px;">This is our demo text in div.
<p>child
<span>grandchild</span>
<span class="test">grandchild</span>
<span>grandchild</span>
</p>
<p>child
<span>grandchild</span>
</p>
</div>
</body>
</html>

輸出

這將產生以下輸出 −

示例

讓我們再看一個示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   .demo * {
      display: block;
      border: 3px solid yellow;
      padding: 3px;
      margin: 25px;
   }
   .one {
      border: 2px solid blue;
   }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("span.test").next().addClass("one");
   });
</script>
</head>
<body>
<h1>Heading One</h1>
<div class="demo" style="width:600px;">This is our demo text in div.
<p>child
<span>grandchild</span>
<span>grandchild</span>
<span>grandchild</span>
<span class="test">grandchild</span>
<span>grandchild</span>
<span>grandchild</span>
</p>
</div>
</body>
</html>

輸出

這將產生以下輸出 −

更新於:2019-11-11

194 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.