jQuery——插件机制

为了扩展jQuery的库函数,jQuery提供了两种方式

1、jQuery.extend(object):扩展jQuery对象本身,主要是用来扩展jQuery全局函数 ,调用时直接$.函数名(参数)


<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>

结果如下:
jQuery——插件机制_第1张图片

2、jQuery.fn.extend(object):扩展 jQuery 元素集,主要用于扩展jQuery插件,调用时需要先创建jQuery对象,然后才能调用相应的函数


<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>

结果如下:
jQuery——插件机制_第2张图片

注意事项:有关匿名函数的部分不是很清楚,暂放在这里有待补充

你可能感兴趣的:(jQuery——插件机制)