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"
  • 相关阅读:
    react native错误排查-TypeError: window.deltaUrlToBlobUrl is not a function
    react native报错处理com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Failed to process
    react native中一次错误排查 Error:Error: Duplicate resources
    umijs开发实践-不同页面交叉使用dva中的modal文件导致的错误
    每天五分钟-javascript数据类型
    react native中使用echarts
    微信小程序中通过腾讯地图进行逆地址解析报错message: "请求来源未被授权, 此次请求来源域名:servicewechat.com"
    在react中实现打印功能
    mac git从代码仓库克隆代码,修改并上传
    基于jwt的用户登录认证
  • 原文地址:https://www.cnblogs.com/qianduanjingying/p/5261940.html
Copyright © 2011-2022 走看看