jquery1.2.6 版本的 nth函数
jQuery.each
1 | next: function(elem){return jQuery.nth(elem,2,"nextSibling");}, |
jquery对象函数next,prev中都用到了jQuery的静态函数nth:1
2
3
4
5
6
7
8
9
10nth: function(cur,result,dir,elem){
result = result || 1;
var num = 0;
for ( ; cur; cur = cur[dir] )
if ( cur.nodeType == 1 && ++num == result )
break;
return cur;
}
jQuery.map
再来看看1.2.6版本的map函数:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15map: function( elems, callback ) {
var ret = [];
// Go through the array, translating each of the items to their
// new value (or values).
//调用callback函数返回新值
for ( var i = 0, length = elems.length; i < length; i++ ) {
var value = callback( elems[ i ], i );
if ( value != null )
ret[ ret.length ] = value;
}
return ret.concat.apply( [], ret );
}
jQuery.unique
1 | unique: function( array ) { |
jQuery.data
在each方法中用到了jQuery.data方法,我们来看看:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25data: function( elem, name, data ) {
elem = elem == window ?
windowData :
elem;
var id = elem[ expando ];
// Compute a unique ID for the element
if ( !id )
id = elem[ expando ] = ++uuid;
// Only generate the data cache if we're
// trying to access or manipulate it
if ( name && !jQuery.cache[ id ] )
jQuery.cache[ id ] = {};
// Prevent overriding the named cache with undefined values
if ( data !== undefined )
jQuery.cache[ id ][ name ] = data;
// Return the named cache data, or the ID for the element
return name ?
jQuery.cache[ id ][ name ] :
id;
}
expando和uuid是这样定义的:1
var expando = "jQuery" + now(), uuid = 0