jquery_1.2.6版本中的nth函数

jquery1.2.6 版本的 nth函数

jQuery.each

1
2
3
4
5
6
7
8
9
10
11
12
13
14
next: function(elem){return jQuery.nth(elem,2,"nextSibling");},
prev: function(elem){return jQuery.nth(elem,2,"previousSibling");}

...省略部分代码
function(name, fn) {
jQuery.fn[ name ] = function( selector ) {
var ret = jQuery.map( this, fn );

if ( selector && typeof selector == "string" )
ret = jQuery.multiFilter( selector, ret );

return this.pushStack( jQuery.unique( ret ) );
};
}

jquery对象函数next,prev中都用到了jQuery的静态函数nth:

1
2
3
4
5
6
7
8
9
10
nth: 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
15
map: 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
unique: function( array ) {
var ret = [], done = {};

try {

for ( var i = 0, length = array.length; i < length; i++ ) {
var id = jQuery.data( array[ i ] );

if ( !done[ id ] ) {
done[ id ] = true;
ret.push( array[ i ] );
}
}

} catch( e ) {
ret = array;
}

return ret;
}

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
25
data: 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

欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%