首页 > 其他 > 详细

jq扩展

时间:2016-07-21 17:45:28      阅读:157      评论:0      收藏:0      [点我收藏+]

一、先了解jQuery extend方法介绍

jQuery的API手册中,extend方法挂载在jQuery和jQuery.fn两个不同对象上方法,但在jQuery内部代码实现的是相同的,只是功能却不太一样;

且看官方给出解释:

jQuery.extend(): Merge the contents of two or more objects together into the first object.(把两个或者更多的对象合并到第一个当中);

jQuery.fn.extend():Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(把对象挂载到jQuery的prototype属性,来扩展一个新的jQuery实例方法)

简单理解两者区别:

jQuery.extend(object); 为扩展jQuery类本身,为自身添加新的方法。

jQuery.fn.extend(object);给jQuery对象添加方法。

1.1、jQuery.extend(object)

<script>
var object1={name: "xy",
                    sex: "男",
                      age: 97};
var object2={birthday:"1997-02-23"};
$.extend(object1,object2);

//$.extend(object2);alert($.birthday);

$(document).ready(function(){
alert(object1.birthday);//输出1997-02-23

});

</script>

1.12、jQuery.fn.extend(object);

(function($){
$.fn.extend({
foo3:function() {
alert(‘对象级别插件extend方式1‘);
},
bar3:function() {
alert(‘对象级别插件extend方式2‘);
}
})
})(jQuery);

$(document).ready(function(){
jQuery.fn.foo3();//输出对象级别插件extend方式1

});

jq扩展

原文:http://www.cnblogs.com/chiyi/p/5692315.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!