jQuery.macros = {
to: {
appendTo: "append",
prependTo: "prepend",
insertBefore: "before",
insertAfter: "after"
},
css: "width,height,top,left,position,float,overflow,color,background".split(","),
filter: [ "eq", "lt", "gt", "contains" ],
attr: {
val: "value",
html: "innerHTML",
id: null,
title: null,
name: null,
href: null,
src: null,
rel: null
},
axis: {
parent: "a.parentNode",
ancestors: jQuery.parents,
parents: jQuery.parents,
next: "jQuery.sibling(a).next",
prev: "jQuery.sibling(a).prev",
siblings: jQuery.sibling,
children: "a.childNodes"
},
each: {
removeAttr: function( key ) {
this.removeAttribute( key );
},
show: function(){
this.style.display = this.oldblock ? this.oldblock : "";
if ( jQuery.css(this,"display") == "none" )
this.style.display = "block";
},
hide: function(){
this.oldblock = this.oldblock || jQuery.css(this,"display");
if ( this.oldblock == "none" )
this.oldblock = "block";
this.style.display = "none";
},
toggle: function(){
$(this)[ $(this).is(":hidden") ? "show" : "hide" ].apply( $(this), arguments );
},
addClass: function(c){
jQuery.className.add(this,c);
},
removeClass: function(c){
jQuery.className.remove(this,c);
},
toggleClass: function( c ){
jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this,c);
},
remove: function(a){
if ( !a || jQuery.filter( [this], a ).r )
this.parentNode.removeChild( this );
},
empty: function(){
while ( this.firstChild )
this.removeChild( this.firstChild );
},
bind: function( type, fn ) {
if ( fn.constructor == String )
fn = new Function("e", ( !fn.indexOf(".") ? "$(this)" : "return " ) + fn);
jQuery.event.add( this, type, fn );
},
unbind: function( type, fn ) {
jQuery.event.remove( this, type, fn );
},
trigger: function( type, data ) {
jQuery.event.trigger( type, data, this );
}
}
};
jQuery.init
通过jQuery.each()往原型中添加marcos的属性作为方法名
init: function(){
jQuery.initDone = true;
jQuery.each( jQuery.macros.axis, function(i,n){
jQuery.fn[ i ] = function(a) {
var ret = jQuery.map(this,n);
if ( a && a.constructor == String )
ret = jQuery.filter(a,ret).r;
return this.pushStack( ret, arguments );
};
});
jQuery.each( jQuery.macros.to, function(i,n){
jQuery.fn[ i ] = function(){
var a = arguments;
return this.each(function(){
for ( var j = 0; j < a.length; j++ )
$(a[j])[n]( this );
});
};
});
jQuery.each( jQuery.macros.each, function(i,n){
jQuery.fn[ i ] = function() {
return this.each( n, arguments );
};
});
jQuery.each( jQuery.macros.filter, function(i,n){
jQuery.fn[ n ] = function(num,fn) {
return this.filter( ":" + n + "(" + num + ")", fn );
};
});
jQuery.each( jQuery.macros.attr, function(i,n){
n = n || i;
jQuery.fn[ i ] = function(h) {
return h == undefined ?
this.length ? this[0][n] : null :
this.attr( n, h );
};
});
jQuery.each( jQuery.macros.css, function(i,n){
jQuery.fn[ n ] = function(h) {
return h == undefined ?
( this.length ? jQuery.css( this[0], n ) : null ) :
this.css( n, h );
};
});
},
原型方法添加过程
jQuery.each( jQuery.macros.axis, function(i,n){
jQuery.fn[ i ] = function(a) {
var ret = jQuery.map(this,n);
if ( a && a.constructor == String )
ret = jQuery.filter(a,ret).r;
return this.pushStack( ret, arguments );
};
});
这里的i是对象的子对象名或者属性名,n是子对象值或者属性值
axis: {
parent: "a.parentNode",
ancestors: jQuery.parents,
.....
这里的意思就是i = parent,n = “a.parentNode”
jQuery.fn[ parent ] = function(a){
var ret = jQuery.map(this,“a.parentNode”);
这里的this是调佣这个方法的对象例如$(‘div’).parent()
这的this = {0:div1,1:div2,2:div3,length:3}
在map里
1:第二个参数字符串会变成函数 fn = function(a){return a.parentNode}
1.1 创建个数组 var res = []
2:for(i = 0;i<this.length:i++){var val = fn(this[i],i)
然后val也就是div.parentNode被包装成数组 val = [val]
再把val这个数组和res数组合并 jQuery.merge(res,val)
最后返回res这个封装了div.parentNode的数组
}
所以ret = 【div1.parentNode,div2.parentNo,div3.parentNo】
}