jQuery.fn.extend()
一个对象的内容合并到jQuery的原型,以提供新的jQuery实例方法。
jQuery.fn.extend()方法继承了jQuery原型($.fn)对象,以提供jQuery原型新的方法,可以链式调用jQuery()函数。Example:
添加两个方法到jQuery原型($.fn),并且使用他们。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.fn.extend demo</title>
<style>
label { display: block; margin: .5em; }
</style>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<label><input type="checkbox" name="foo"> Foo</label>
<label><input type="checkbox" name="bar"> Bar</label>
<script>
jQuery.fn.extend({
check: function () {
return this .each(function () { this .checked = true; });
},
uncheck: function () {
return this .each(function () { this .checked = false; });
}
});
// Use the newly created .check() method
$( "input[type='checkbox']" ).check();
</script>
</body>
</html>
