Home  >  Article  >  Web Front-end  >  浅谈jquery.fn.extend与jquery.extend区别_jquery

浅谈jquery.fn.extend与jquery.extend区别_jquery

WBOY
WBOYOriginal
2016-05-16 15:50:331197browse

1.jquery.extend(object); 为扩展jQuery类本身.为类添加新的方法。
jquery.fn.extend(object);给jQuery对象添加方法。

$.extend({ 
  add:function(a,b){return a+b;} 
}); 

//$.add(3,4);
//return 7 

jQuery添加一个为 add的“静态方法”,之后便可以在引入 jQuery 的地方,使用这个方法了.

2.jQuery.fn.extend(object); 对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”。jQuery类的实例可以使用这个“成员函数”。

$.fn.extend({ 
  alertClick:function(){ 
    $(this).click(function(){ 
      alert($(this).val()); 
    }); 
  } 
}); 

//页面上为:
<input id="input1" type="text"/>    

//使用
$("#input1").alertClick();  

以上所述就是本文的全部内容了,希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn