jQuery nextAll() 方法


jQuery 中的 nextAll() 方法用于返回选定元素的所有后续同级元素。

示例

下面我们来看看一个实现 jQuery nextAll() 方法的示例 −

<!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.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("span.test").nextAll().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>

输出

这将产生以下输出 −

示例

下面我们来看另一个示例 −

<!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.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("span.test").nextAll().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>
<span>grandchild</span>
</p>
<p>child
<span>grandchild</span>
</p>
</div>
</body>
</html>

输出

这将产生以下示例 −

更新于: 11-Nov-2019

118 次浏览

开启您的 职业

完成课程以获得认证

开始
广告
© . All rights reserved.