By
lyuan
1月 22 2015
更新日期:5月 4 2015
工具方法列表
$.each
$.trim
$.makeArray
$.inArray
$.merge
$.grep
$.map
$.proxy
$.each
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$.each = function ( obj, callback, args ) {
var value,
i = 0 ,
length = obj.length,
isArray = isArraylike( obj );
if ( args ) {
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break ;
}
}
} else {
for ( i in obj ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break ;
}
}
}
} else {
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback.call( obj[ i ], i, obj[ i ] );
if ( value === false ) {
break ;
}
}
} else {
for ( i in obj ) {
value = callback.call( obj[ i ], i, obj[ i ] );
if ( value === false ) {
break ;
}
}
}
}
return obj;
}
用法:
$.makeArray
转换一个类似数组的对象成为真正的JavaScript数组
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
26
27
28
29
30
31
32
33
34
35
$.makeArray = function ( arr, results ) {
var ret = results || [];
if ( arr != null ) {
if ( isArraylike( Object (arr) ) ) {
jQuery.merge( ret,
typeof arr === "string" ?
[ arr ] : arr
);
} else {
core_push.call( ret, arr );
}
}
return ret;
}
function isArraylike ( obj ) {
var length = obj.length,
type = jQuery.type( obj );
if ( jQuery.isWindow( obj ) ) {
return false ;
}
if ( obj.nodeType === 1 && length ) {
return true ;
}
return type === "array" || type !== "function" &&
( length === 0 ||
typeof length === "number" && length > 0 && ( length - 1 ) in obj );
}
$.merge
合并两个数组(类数组)内容到第一个数组,第二个数组不会被修改; 代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$.merge = function ( first, second ) {
var l = second.length,
i = first.length,
j = 0 ;
if ( typeof l === "number" ) {
for ( ; j < l; j++ ) {
first[ i++ ] = second[ j ];
}
} else {
while ( second[j] !== undefined ) {
first[ i++ ] = second[ j++ ];
}
}
first.length = i;
return first;
}
$.grep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$.grep = function ( elems, callback, inv ) {
var retVal,
ret = [],
i = 0 ,
length = elems.length;
inv = !!inv;
for ( ; i < length; i++ ) {
retVal = !!callback( elems[ i ], i );
if ( inv !== retVal ) {
ret.push( elems[ i ] );
}
}
return ret;
}
$.map
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
26
27
28
29
30
31
32
$.map = function ( elems, callback, arg ) {
var value,
i = 0 ,
length = elems.length,
isArray = isArraylike( elems ),
ret = [];
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback( elems[ i ], i, arg );
if ( value != null ) {
ret[ ret.length ] = value;
}
}
} else {
for ( i in elems ) {
value = callback( elems[ i ], i, arg );
if ( value != null ) {
ret[ ret.length ] = value;
}
}
}
return core_concat.apply( [], ret );
}
$.proxy
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
26
27
28
$.proxy = function ( fn, context ) {
var tmp, args, proxy;
if ( typeof context === "string" ) {
tmp = fn[ context ];
context = fn;
fn = tmp;
}
if ( !jQuery.isFunction( fn ) ) {
return undefined ;
}
args = core_slice.call( arguments , 2 );
proxy = function () {
return fn.apply( context || this , args.concat( core_slice.call( arguments ) ) );
};
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
return proxy;
}
jQuery 工具方法
$.parseHTML() — Parses a string into an array of DOM nodes
jQuery.parseHTML 使用原生的DOM元素的创建函数将字符串转换为一组DOM元素,然后,可以插入到文档中。 默认情况下,如果没有指定或给定null or undefined,context是当前的document。如果HTML被用在另一个document中,比如一个iframe,该frame的文件可以使用。
$.parseHTML Example
1
2
3
4
5
6
7
8
str = "hello, <b>my name is</b> jQuery." ,
html = $.parseHTML( str ); --> [<TextNode textContent ="hello, " > , b, <TextNode textContent =" jQuery." > ]
for(var i=0,len=html.length; i<len; i ++){
console.log (html [i ],html [i ].nodeName ); -- > <TextNode textContent ="hello, " > #text
<b > B
<TextNode textContent =" jQuery." > #text
}
$.parseHTML 源码实现
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
26
27
28
29
30
31
32
33
$.parseHTML = function ( data, context, keepScripts ) {
if ( !data || typeof data !== "string" ) {
return null ;
}
if ( typeof context === "boolean" ) {
keepScripts = context;
context = false ;
}
context = context || document ;
var parsed = rsingleTag.exec( data ),
scripts = !keepScripts && [];
if ( parsed ) {
return [ context.createElement( parsed[1 ] ) ];
}
parsed = jQuery.buildFragment( [ data ], context, scripts );
if ( scripts ) {
jQuery( scripts ).remove();
}
return jQuery.merge( [], parsed.childNodes );
}