jQuery为开发插件提拱了两个方法,分别是:html
jQuery.extend(object)ide
jQuery.fn.extend(object)函数
jQuery.extend(object); 为扩展jQuery类自己.为类添加新的方法。能够理解为添加静态方法this
示例以下,返回两个数种较大的一个spa
$.extend({prototype
Max:function(a,b){插件
if(a>b){htm
return a;对象
}else{开发
return b;
}
}
});
调用方法:
var max=$.Max(10,100);//返回两个数种较大的一个
jQuery.fn.extend(object);给jQuery对象添加方法,对jQuery.prototype进行扩展,就是为jQuery类添加“成员函数”。jQuery类的实例可使用这个“成员函数”。
查看fn的jQuery代码以下:
jQuery.fn=jQuery.prototype={
init:function(select,context){}
};
发现jQuery.fn = jQuery.prototype,是对其提供扩展方法,
下面使用jQuery.fn开发一个小插件,但文本框获取焦点之后清空文本框的内容
jQuery.fn.extend({
cleartext:function(){
$(this).focus(function(){
$(this).val("");
});
}
});
调用方法以下:
$(document).ready(function(){
$("input[type='text']").cleartext();
});
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="input username" />
</body>
</html>