/
jquery.classlist.js
68 lines (52 loc) · 1.55 KB
/
jquery.classlist.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*!
* jQuery ClassList Plugin v0.5.0
* http://github.com/rwldrn/jquery-classlist
*
* Copyright (c) 2010 Rick Waldron
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function( jQuery, undefined ) {
var slice = [].slice;
// Determine classList support
jQuery.support.classList = !!document.createElement( "div" ).classList;
// Provide a browser compatible implementation of the classList api
jQuery.fn.classlist = function( value ) {
var elem = this[0],
iter = 0,
classNames, className, len;
// Getter logic
if ( !value ) {
// Native classList is an array-like object; for normalization
// with non-native implementations, we return arrays
// This approach has proven significantly faster
classNames = jQuery.support.classList ?
elem.classList.toString() :
elem.className;
return !jQuery.trim( classNames ) ? [] : ( classNames || "" ).split( " " );
}
if ( jQuery.type(value) === "array" ) {
len = this.length;
while ( len-- ) {
elem = this[ iter++ ];
if ( elem.nodeType === 1 ) {
if ( !elem.className ) {
elem.className = value.join(" ");
} else {
className = " " + elem.className + " ";
setClass = elem.className;
for ( var c = 0, cl = value.length; c < cl; c++ ) {
if ( jQuery.support.classList ) {
elem.classList.add( value[ c ] );
} else {
if ( className.indexOf( " " + value[ c ] + " " ) < 0 ) {
elem.className += " " + value[ c ];
}
}
}
}
}
}
}
return this;
};
})( jQuery );