以下的文件都要看html的源码 jquery 解剖 源码解析 jquery解析
1,为什么我们能用$().css()类似这样的方法去写代码呢?
推荐看以下:
(function(window,undefind){ //第一参数是window全局对象。第二参数是undefind,但偏偏不给它赋值,因为不同浏览器是可以修改undefind的值的,不给它传值是为了保证undefind的值就是undefind
var jQuery=function(){ //此函数是自调用,里面的代码会被自动执行。如果此函数被调用jQuery()相当于执行了new jQuery.fn.init(selector,context)
var jQuery=function(selector,context){ //声明一个jQuery函数
return new jQuery.fn.init(selector,context); //我们平时写的$() ,其实就是调用这里所返回给我们的
};
jQuery.fn=jQuery.prototype={ //在jquery的原型对象上扩展方法和属性 ,基本用法都是$().xx()
constructor:jQuery, //要使用字面量的书写方式给原型对象添加属性和方法,就要constructor属性来指定构造函数,不然是找不到的
init:function(){},
jquery:"1.7.1",
eq:function(){},
size:function(){}
//...
};
jQuery.fn.init.prototype=jQuery.fn; //只需要理解,老爸给财产儿子,儿子继承老爸财产。以后只要在老爸身上添加属性和方法自然儿子也就有老爸的属性和方法了
jQuery.extend=jQuery.fn.extend=function(){
this['xx']=arguments[0].xx
this['yy']=arguments[0].yy
// Return the modified object
return this;
} //同时在jquery构造函数和jquery原型对象添加同样的方法extend
jQuery.extend({ //相当于在jquery这个构造函数上添加属性和方法,调用基本是$.xx()。如果是基于jQuery.fn.extend扩展的话,就相当于为jQuery扩展功能了,一般调用都是$().xx()
xx:function(){ document.title=document.title+'1111'+$().jquery },
yy:function(){ document.title=document.title+'yyyy' },
ajax:function(){}
});
return jQuery;
}(); //结果是 jQuery = jQuery;
window.jQuery=window.$=jQuery //把jquery函数赋值给window.jQuery和window.$;这里的jQuery其实就是return jQuery,一旦jQuery()就相当于new jQuery.fn.init(selector,context)
})(window); //将window对象传入,匿名函数中,免得去找省时间
$() //相当于执行了jQuery() ,又相当于执行了 new jQuery.fn.init(selector,context)