Js-Html 前端系列--全选,反选

本文介绍了一种使用JavaScript实现全选和反选功能的方法,并通过具体代码示例展示了如何为一组复选框设置全选与反选行为。此外,还提供了一个简单的计时器实现方案,利用setTimeout函数创建了两个首尾相连的定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 全选 */
    $("#selectedAll").click(function(){
        var boxcList = $(".boxc");var boxcList1 = $(".boxcp");
        var c = $(this).attr("checked");
        if(c == "undefined"||c == undefined||c == false){
            boxcList.prop("checked",true);
            boxcList.attr("checked",true);
            boxcList1.prop("checked",true);
            boxcList1.attr("checked",true);
            $(this).prop("checked",true);
            $(this).attr("checked",true);
        }else{
            boxcList.removeAttr("checked");
            boxcList1.removeAttr("checked");
            $(this).removeAttr("checked");
        }


    });
    /* 反选 */
    $("#backselectedAll").click(function(){
        var boxcList = $(".boxc");var boxcList1 = $(".boxcp");
        for(var i=0;i<boxcList.length;i++){
            var c = boxcList.eq(i).attr("checked");
            if(c == "undefined"||c == undefined||c == false){
                boxcList.eq(i).prop("checked",true);
                boxcList.eq(i).attr("checked",true);
            }else{
                boxcList.eq(i).removeAttr("checked");
            }
        }
        for(var i=0;i<boxcList1.length;i++){
            var c = boxcList1.eq(i).attr("checked");
            if(c == "undefined"||c == undefined||c == false){
                boxcList1.eq(i).prop("checked",true);
                boxcList1.eq(i).attr("checked",true);
            }else{
                boxcList1.eq(i).removeAttr("checked");
            }
        } 
    });

Js产生两个首尾相连的记时器

<script>
var time=0;
 setTimeout(a(), 1000);
 alert("开始")
 function b(){
     alert("么么哒 "+time++)
     setTimeout(a(), 5000); 
 }
 function a(){
     alert("萌萌哒 "+time++)
     setTimeout(b(), 5000); 
 }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洲上牧童

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值