<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加两个方法到jQuery原型($.fn)</title>
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
    label {
        display: block;
        margin: .5em;
    }
</style>
</head>
<body>
 
<label><input type="checkbox" name="foo"> Foo</label>
<label><input type="checkbox" name="bar"> Bar</label>
<script>
$(function () { 
    $.fn.extend({
        check: function() {
            return this.each(function() {
                this.checked = true;
            });
        },
        uncheck: function() {
            return this.each(function() {
                this.checked = false;
            });
        }
    });
    // 使用新创建的.check() 方法
    $( "input[type='checkbox']" ).check();
})
</script>
 
</body>
</html>

标签:jQuery

你的评论