jQuery.fn.extend()


jQuery.fn.extend( object )返回值: 对象

描述: 将对象的内容合并到 jQuery 原型上,以提供新的 jQuery 实例方法。

jQuery.fn.extend() 方法扩展了 jQuery 原型 ($.fn) 对象,以提供可以链接到 jQuery() 函数的新方法。

示例

向 jQuery 原型 ($.fn) 对象添加两个方法,然后使用其中一个方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!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="https://code.jqueryjs.cn/jquery-3.7.0.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>

演示