jQuery的next()、nextAll()、nextUntil()方法

.next()

作用:选择指定元素的同级的下一个元素

【例】代码功能,点击一个方块,使下一个方块的样式改变

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
div {
    background: linear-gradient(to right, steelblue, cadetblue );
    width: 200px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    color: #fff;
    margin-bottom: 5px;
    cursor: pointer;
}
.active {
    border-radius: 10px;
    background: linear-gradient(to right, teal, darkseagreen);
}
    </style>
</head>
<body>
    <div>夕阳很美</div>
    <div>美也没用</div>
    <div>没用也美</div>
</body>
<script src="./jquery.js"></script>
<script>
    $('div').click(function () {
        $(this).next().addClass('active');
    })
</script>
</html>

【注】next()也可传参数,若指定元素的下一个元素具备next的过滤条件,则可选中该元素,否则不能选中

.nextAll()

作用:选中同级中所有下面的子元素节点

【例】全选的复选框,代码效果如下

代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 350px;
            height: 80px;
            background: linear-gradient(to bottom, cadetblue, steelblue);
            color: #fff;
            text-align: center;
        }
        h1 {
            font-size: 20px;
            text-align: center;
        }
        .all {
            margin-right: 20px;
        }
        .submit {
            width: 50px;
            height: 20px;
            line-height: 20px;
            background-color:#eee;
            color: #424242;
            border: none;
            outline: none;
            border-radius: 5px;
            margin-left: 20px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <h1>你喜欢的水果</h1>
        全选<input type="checkbox" name="all" class="all">
        苹果<input type="checkbox" name="1">
        橘子<input type="checkbox" name="2">
        香蕉<input type="checkbox" name="3">
        <input type="submit" value="提交" class="submit">
    </div>
</body>
<script src="./jquery.js"></script>
<script>
    $('.all').click(function () {
        if ($('.all').prop('checked')) {    //判断该复选框是否被选中
            $(this).nextAll('input[type=checkbox]').prop('checked',true);   //若被选中则所有复选框都被选中
        } else {
            $(this).nextAll('input[type=checkbox]').prop('checked',false);  //反之则所有复选框未被选中
        }
    })
</script>
</html>

【注】需在nextAll中添加过滤条件,否则下面所有同级的元素都添加check属性。

.nextUntil()

作用:设指定元素为A,nextUntil方法的参数选中的是B,则该方法可选中从A到B之间的所有元素

【例】多个全选复选框

代码效果

 

代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrapper {
            width: 350px;
            height: 500px;
            background: linear-gradient(to bottom, cadetblue, steelblue);
            color: #fff;
            padding: 10px 5px 20px 5px;
            margin-bottom: 5px;
        }
        h1 {
            font-size: 22px;
            margin: 0;
            padding: 0;
            text-align: center;
        }
        h2 {
            font-size: 20px;
            text-align: center;
            text-align: center;
            padding: 0;
            margin: 10px;
        }
        h3 {
            font-size: 18px;
            text-align: left;
        }
        .all {
            margin-right: 20px;
        }
        .submit {
            width: 50px;
            height: 20px;
            line-height: 20px;
            background-color:#eee;
            color: #424242;
            border: none;
            outline: none;
            border-radius: 5px;
            margin-left: 20px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <h1>你的喜欢</h1>
        全选<input type="checkbox" name="" id="">
        <h2>你喜欢的食物</h2>
        全选<input type="checkbox" name="" id="" class="choseAll">
        <h3>你喜欢的水果</h3>
        全选<input type="checkbox" name="all" class="all">
        苹果<input type="checkbox" name="1">
        橘子<input type="checkbox" name="2">
        香蕉<input type="checkbox" name="3">
        <h3>你喜欢的饮料</h3>
        全选<input type="checkbox" name="all" class="all">
        奶茶<input type="checkbox" name="1">
        可乐<input type="checkbox" name="2">
        酸奶<input type="checkbox" name="3">
        <input type="submit" value="提交" class="submit">

        <h2>你喜欢的娱乐项目</h2>
        全选<input type="checkbox" name="" id="" class="choseAll">
        <h3>你喜欢的游戏</h3>
        全选<input type="checkbox" name="all" class="all">
        王者荣耀<input type="checkbox" name="1">
        和平精英<input type="checkbox" name="2">
        第五人格<input type="checkbox" name="3">
        <h3>你喜欢的软件</h3>
        全选<input type="checkbox" name="all" class="all">
        抖音<input type="checkbox" name="1">
        快手<input type="checkbox" name="2">
        头条<input type="checkbox" name="3">
        <input type="submit" value="提交" class="submit">
    </div>
</body>
<script src="./jquery.js"></script>
<script>
    $('.wrapper').find('input').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);  
        }
    })
    $('h2').next().click(function () {
        if ($(this).prop('checked')) {
            $(this).nextUntil('input[type=submit]').prop('checked',true);   
        } else {
            $(this).nextUntil('input[type=submit]').prop('checked',false);  
        }
    })
    $('h3').next().click(function () {
        if ($(this).prop('checked')) {
            $(this).nextUntil('h3', 'input[type=checkbox]').prop('checked',true);   
        } else {
            $(this).nextUntil('h3', 'input[type=checkbox]').prop('checked',false);  
        }
    })
</script>
</html>

【注】nextInput可传两个参数,第一个参数可确定选择区间,第二个参数可过滤区间内不需要选中的元素

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值