为了扩展jQuery的库函数,jQuery提供了两种方式
1、jQuery.extend(object):扩展jQuery对象本身,主要是用来扩展jQuery全局函数 ,调用时直接$.函数名(参数)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="js/jquery-1.8.3.js" ></script>
<script>
jQuery.extend({
max:function(a,b){
return a>b?a:b;
},
min:function(a,b){
return a>b?b:a;
}
})
console.log($.max(6,9));
console.log($.min(6,9));
</script>
</head>
<body>
</body>
</html>
结果如下:
2、jQuery.fn.extend(object):扩展 jQuery 元素集,主要用于扩展jQuery插件,调用时需要先创建jQuery对象,然后才能调用相应的函数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="js/jquery-1.8.3.js" ></script>
<script>
$(function(){
jQuery.fn.extend({
check:function(){
this.attr("checked","checked");
}
})
$("[type='checkbox']").check();
})
</script>
</head>
<body>
<input type="checkbox" /><input type="checkbox" /><input type="checkbox" />
</body>
</html>
结果如下: