jQuery核心(一)

API文档:http://jquery.cuishifeng.cn/

一、 jQuery.extend(object)

用于扩展jQuery对象本身。用来在jQuery命名空间上增加新函数。

jQuery.extend({
  min: function(a, b) { return a < b ? a : b; },
  max: function(a, b) { return a > b ? a : b; }
});
jQuery.min(2,3); // => 2
jQuery.max(4,5); // => 5

二、jQuery.fn.extend(object)

扩展 jQuery 元素集来提供新的方法(通常用来制作插件)。

jQuery.fn.extend({
  check: function() {
    return this.each(function() { this.checked = true; });
  },
  uncheck: function() {
    return this.each(function() { this.checked = false; });
  }
});

$("input[type=checkbox]").check();
$("input[type=radio]").uncheck();

$.extend(object)  和 $.fn.extend(object)  的区别:

  • $.extend(object) 可以理解为JQuery 添加一个静态方法。
  • $.fn.extend(object) 可以理解为JQuery实例添加一个方法。

jQuery(function () { }); 与  (function ($) { })(jQuery);的区别:

jQuery(function () { });
//相当于
$(document).ready(function () { });
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
(function ($) { })(jQuery);
//相当于
var fn = function ($) { };
fn(jQuery);

jQuery(function () { });是某个DOM元素加载完毕后执行方法里的代码。

(function ($) { })(jQuery); 定义了一个匿名函数,其中jQuery代表这个匿名函数的实参。通常用在JQuery插件开发中,起到了定义插件的私有域的作用。

猜你喜欢

转载自www.cnblogs.com/myitnews/p/11780631.html