jquery常用方法之next()、prev()、nextAll、prevAll方法

next ( ) : 获取被选中元素的下一个同级元素节点;

next ( )中也可以添加条件,如:选出div下一个且必须是p标签的元素

  <div class="div1"></div>
  <p class="p1"></p>
  <div class="div2"></div>
<script>
       $(".div").next("p")
</script>

prev ( ) : 获取被选中元素的上一个同级元素节点;

nextAll ( ): 获取被选中元素的下边所有同级元素节点;

nextAll( )中也可以添加条件,如下:

选出input下边所有且有type="checkbox"属性的元素,(一个全选功能,关于prop的用法上一篇文章有介绍):

全选<input type="checkbox"><br>
财富<input type="checkbox">
地位<input type="checkbox">
权利<input type="checkbox">
 <script>
     $("input[type='checkbox']").eq(0).click(function(){
         if( $(this).prop("checked") ){//判断下 全选框是否被选中,返回的是布尔值
             $(this).nextAll("input[type='checkbox']").prop("checked",true);
         }else{
             $(this).nextAll("input[type='checkbox']").prop("checked",false);
         }
     })
 </script>

效果如下:

prevAll ( ): 获取被选中元素的上边所有同级元素节点;

nextUntil ( ): 获取被选中元素下边所有同级且有type="checkbox"属性的元素直到h1为止,括号中填写条件;如下:

nextUntil(param1,param2),这里边两个参数,第一个参数表示:选择范围;第二个参数表示:选择的是什么类型的元素;

        <div>
           全选<input type="checkbox"><br>
               <input type="checkbox">
               <input type="checkbox">
               <h1>title</h1>
               <input type="checkbox">
               <input type="checkbox">
        </div>
    <script>
        $("input[type='checkbox']").nextUntil("h1","input[type='checkbox']")
    </script>

猜你喜欢

转载自blog.csdn.net/qq_42778001/article/details/90343462