zoukankan      html  css  js  c++  java
  • jqeury实现全选和反选

    注意:对于input获取属性不能用attr(),只能用prop()。不然出现undefined。

    第一版:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>全选、全不选、反选</title>
    </head>
    <body>
        <label><input type="checkbox" name="" id="">衣服</label>
        <label><input type="checkbox" name="" id="">鞋子</label>
        <label><input type="checkbox" name="" id="">裤子</label>
        <label><input type="checkbox" name="" id="">内衣</label>
        <input id="btn1" type="button" value="全选">
        <input id="btn2" type="button" value="反选">
        <script type="text/javascript" src="../jquery-1.11.1.min.js"></script>
        <script type="text/javascript">
            $('#btn1').click(function(event) {
            //先保存这些checkbox的这个属性的值,取反。
    var check = $(":checkbox").prop('checked'); $(":checkbox").prop('checked',!check); }); $('#btn2').click(function(event) { $(":checkbox").each(function(index) {
            //遍历每一个checkbox,针对当前的这个取反。
    var check = $(this).prop('checked'); $(this).prop('checked',!check); }); }); </script> </body> </html>

    第二版:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>全选、全不选、反选</title>
    </head>
    <body>
        <label><input type="checkbox" name="" id="">衣服</label>
        <label><input type="checkbox" name="" id="">鞋子</label>
        <label><input type="checkbox" name="" id="">裤子</label>
        <label><input type="checkbox" name="" id="">内衣</label>
        <input id="btn1" type="button" value="全选">
        <input id="btn2" type="button" value="反选">
        <script type="text/javascript" src="../jquery-1.11.1.min.js"></script>
        <script type="text/javascript">
            $('#btn1').click(function(event) {
                if($(":checkbox").prop('checked')){
                    $(":checkbox").prop('checked',false);
                    $(this).attr('value','全选');
                }else{
                    $(":checkbox").prop('checked',true);
                    $(this).attr('value','全不选');
                }
            });
    
            $('#btn2').click(function(event) {
                $(":checkbox").each(function(index) {
                    var check = $(this).prop('checked');
                    console.log(check);
                    $(this).prop('checked',!check);
                });
            });
        </script>
    </body>
    </html>

    在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了。

    关于它们两个的区别,网上的答案很多。这里谈谈我的心得,我的心得很简单:

    • 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
    • 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。

    上面的描述也许有点模糊,举几个例子就知道了。 

    <a href="http://www.baidu.com" target="_self" class="btn">百度</a>

     这个例子里<a>元素的DOM属性有“href、target和class",这些属性就是<a>元素本身就带有的属性,也是W3C标准里就包含有这几个属性,或者说在IDE里能够智能提示出的属性,这些就叫做固有属性。处理这些属性时,建议使用prop方法。

    <a href="#" id="link1" action="delete">删除</a>

    这个例子里<a>元素的DOM属性有“href、id和action”,很明显,前两个是固有属性,而后面一个“action”属性是我们自己自定义上去的,<a>元素本身是没有这个属性的。这种就是自定义的DOM属性。处理这些属性时,建议使用attr方法。使用prop方法取值和设置属性值时,都会返回undefined值。

    再举一个例子:

    <input id="chk1" type="checkbox" />是否可见
    <input id="chk2" type="checkbox" checked="checked" />是否可见

    像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此需要使用prop方法去操作才能获得正确的结果。

    $("#chk1").prop("checked") == false
    $("#chk2").prop("checked") == true

    如果上面使用attr方法,则会出现:

    $("#chk1").attr("checked") == undefined
    $("#chk2").attr("checked") == "checked"
  • 相关阅读:
    转《编程之美——微软技术面试心得》勘误表
    第一次 学习使用 智能指针
    test _todel
    (转 todo阅读)Android 官方博客 Android应用程序的内存分析(翻译)
    msdn snmp trap 研究可否 重入 转《Multiple Trap Registrations》
    wpbars在博客园开博客了
    创业失败的10个教训总结
    winform 的一种登录方法。
    快速建立Subversion
    (转)SQL Server 按某一字段分组取最大(小)值所在行的数据
  • 原文地址:https://www.cnblogs.com/qianduanjingying/p/5261940.html
Copyright © 2011-2022 走看看