美文网首页jQuery
自定义插件-实现check radio 反选

自定义插件-实现check radio 反选

作者: 丁小时候 | 来源:发表于2016-09-18 23:48 被阅读42次

    `<!DOCTYPE html>
    <html>
    <head>
    <title>自定义插件-实现check radio 反选</title>
    <meta charset="utf-8">

    </head>
    <body>
    <script type="text/javascript" src = "jquery-2.2.3.min.js"></script>

    <input type="checkbox" name="">
    <input type="checkbox" name="">
    <input type="radio" name="">
    <input type="radio" name="">
    <input type="checkbox" name="">
    <input type="checkbox" name="">
    <button id = "span">点击反选</button>
    
    <script type="text/javascript">
        $.fn.extend({
            aa:function(ele){
                //
                $(this).on('click',function(){
                    //方法一  each 遍历,改变状态
                    // ele.each(function(){
                    //  //获取 控制的check , prop 获取属性 并 赋值 为 它当前 属性值的相反值
                    //  $(this).prop('checked',!$(this).prop('checked'));
                    // })
    
                    //方法二  分别操作 checkbox  radio 不可以????
                    $("checkbox").prop("checked",!$("checkbox").prop("checked"));
    
                    $("radio").prop("checked",!$("radio").prop("checked"));
                })
            }
        });
        //调用方法一:
        // 通过 id 名为  span  获取
        // $('#span').aa($('input[type = checkbox],input[type = radio]'));
    
        //调用方法二:
        //通过 按钮来 调用  自定义的插件方法
        $('button').aa($('input'));
    </script>
    

    </body>
    </html>`

    相关文章

      网友评论

        本文标题:自定义插件-实现check radio 反选

        本文链接:https://www.haomeiwen.com/subject/pybgettx.html