jQuery自定义插件

jQuery自定义插件

jQuery.prototype.sayHello = function(){
   console.log("hello");
}
$(document).sayHello();
//相当于
$.fn.sayHello = function(){
	console.log("hello");
}

直接给原对象增加一个方法,那么所有的对象都可以使用这个方法

例子:
新建一个bgColor的方法

$(function(){
  $.fn.bgColor = function(color){
  	this.css("backgroundColor",color);
  };  
  $("div").bgColor("red");
});
posted @ 2017-09-06 09:39  Bilyooyam  阅读(97)  评论(0编辑  收藏  举报