jQuery的eq选择器+全选和反选


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
    <table border="2">
        <tr>
            <td><input type="checkbox" value="1"></td>
            <td>豹女</td>
            <td>300</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="2"></td>
            <td>龙女</td>
            <td>300</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="3"></td>
            <td>提摩</td>
            <td>320</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="4"></td>
            <td>琴女</td>
            <td>350</td>
        </tr>
    </table>
 
    <input type="button" value="点击选择第几个使用eq选择器" id="eqBtn">
    <input type="button" value="点击选择第1个使用first选择器" id="firstBtn">
    <input type="button" value="点击选择最后一个使用first选择器" id="lastBtn">
    <input type="button" value="全选" id="allBtn">
    <input type="button" value="点击查看,已经选中的" id="checkedBtn">
    <input type="button" value="点击查看,没有选中的" id="nocheckedBtn">
    <input type="button" value="反选" id="overBtn">
    <input type="button" value="反选升级版" id="overBtn1">
 
    <script>
        $(function() {
            //jquery使用 过滤选择器 达到 奇偶数变色
            //同时也学了 jquery操作css
            $('table tr:even').css('background-color','pink');
            $('table tr:odd').css('background-color','blue');
 
            $('#eqBtn').click(function() {
              var timo=$('table tr:eq(3)');
              //console.log(timo);
              //练习:如何找到 <td>提摩</td>
              //var timo=$('table tr:eq(2) td:eq(1) ').text();
              console.log(timo);
            })
            //拿取 第一个
            $("#firstBtn").click(function() {
              var first=$('table tr:first').html();
              console.log(first);
            })
            //拿取  最后一个
            $("#lastBtn").click(function() {
              var last=$('table tr:last').text();
              console.log(last);
            })
            //全选  -----用来批量删除
            $('#allBtn').click(function() {
                //思路:找出所有  cheackbox 的td,遍历 ,让其选中 即可
              // $('table tr td>input')
              $.each($('table tr td>input'),function(index, value) {
                //   console.log(index); 
                //   console.log(value);
                  console.log( $(this).val())  //遍历取值  
                  $(this).prop('checked',true);  //全选
              })
            })
 
        //点击查看  已经选中的
        $('#checkedBtn').click(function() {
            //使用 过滤选择器  可以选中:
            //$('table tr td>input:checked')
            //遍历 取值
            $.each($('table tr td>input:checked'), function(index, value) {
                console.log($(this).val());
            })   
 
       })
 
        //点击查看  没有选中的
        $('#nocheckedBtn').click(function() {
            console.log($('table tr td>input:not(:checked)').val());
 
        }) 
            
            //反选
            $('#overBtn').click(function() {
                //思路:遍历每个 input ,如果是 选中,则给予 未选中
                //反过来,如果是 未选中,则给予 选中
                $.each($('table tr td>input'), function(index, value) {  
                 var istrue=$(this).prop('checked');
                 console.log(istrue);
                 if(istrue){
                    //如果 istrue 等于 true  代表被选中了
                    $(this).prop('checked',false);
                 }else{
                    $(this).prop('checked',true);
                 }
                
              })
            })
            //升级版
            $('#overBtn1').click(function() {
              $.each($('table tr td>input'), function(index, value) {
                  $(this).prop('checked',!($(this).prop('checked')));   
              })  
            })
 
        })
 
 
    </script>
 
 
 
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值