@@ -12,10 +12,27 @@ jQuery.fn.extend({
1212 return access ( this , name , value , true , jQuery . attr ) ;
1313 } ,
1414
15+ removeAttr : function ( name ) {
16+ if ( jQuery . isFunction ( name ) ) {
17+ return this . each ( function ( i ) {
18+ var self = jQuery ( this ) ;
19+ self . removeAttr ( name . call ( this , i , self . attr ( name ) ) ) ;
20+ } ) ;
21+ }
22+
23+ return this . each ( function ( ) {
24+ jQuery . attr ( this , name , "" ) ;
25+ if ( this . nodeType === 1 ) {
26+ this . removeAttribute ( name ) ;
27+ }
28+ } ) ;
29+ } ,
30+
1531 addClass : function ( value ) {
1632 if ( jQuery . isFunction ( value ) ) {
17- return this . each ( function ( ) {
18- jQuery ( this ) . addClass ( value . call ( this ) ) ;
33+ return this . each ( function ( i ) {
34+ var self = jQuery ( this ) ;
35+ self . addClass ( value . call ( this , i , self . attr ( "class" ) ) ) ;
1936 } ) ;
2037 }
2138
@@ -46,8 +63,9 @@ jQuery.fn.extend({
4663
4764 removeClass : function ( value ) {
4865 if ( jQuery . isFunction ( value ) ) {
49- return this . each ( function ( ) {
50- jQuery ( this ) . removeClass ( value . call ( this ) ) ;
66+ return this . each ( function ( i ) {
67+ var self = jQuery ( this ) ;
68+ self . removeClass ( value . call ( this , i , self . attr ( "class" ) ) ) ;
5169 } ) ;
5270 }
5371
@@ -75,6 +93,40 @@ jQuery.fn.extend({
7593 return this ;
7694 } ,
7795
96+ toggleClass : function ( classNames , state ) {
97+ var type = typeof classNames ;
98+
99+ if ( jQuery . isFunction ( classNames ) ) {
100+ return this . each ( function ( i ) {
101+ var self = jQuery ( this ) ;
102+ self . toggleClass ( classNames . call ( this , i , self . attr ( "class" ) ) , state ) ;
103+ } ) ;
104+ }
105+
106+ return this . each ( function ( ) {
107+ if ( type === "string" ) {
108+ // toggle individual class names
109+ var isBool = typeof state === "boolean" , className , i = 0 ,
110+ classNames = classNames . split ( rspace ) ;
111+
112+ while ( ( className = classNames [ i ++ ] ) ) {
113+ // check each className given, space seperated list
114+ state = isBool ? state : ! jQuery ( this ) . hasClass ( className ) ;
115+ jQuery ( this ) [ state ? "addClass" : "removeClass" ] ( className ) ;
116+ }
117+
118+ } else if ( type === "undefined" || type === "boolean" ) {
119+ if ( this . className ) {
120+ // store className if set
121+ jQuery . data ( this , "__className__" , this . className ) ;
122+ }
123+
124+ // toggle whole className
125+ this . className = this . className || classNames === false ? "" : jQuery . data ( this , "__className__" ) || "" ;
126+ }
127+ } ) ;
128+ } ,
129+
78130 hasClass : function ( selector ) {
79131 var className = " " + selector + " " ;
80132 for ( var i = 0 , l = this . length ; i < l ; i ++ ) {
@@ -149,9 +201,11 @@ jQuery.fn.extend({
149201
150202 var val = value ;
151203
152- return this . each ( function ( ) {
204+ return this . each ( function ( i ) {
205+ var self = jQuery ( this ) ;
206+
153207 if ( jQuery . isFunction ( value ) ) {
154- val = value . call ( this ) ;
208+ val = value . call ( this , i , self . val ( ) ) ;
155209
156210 // Typecast each time if the value is a Function and the appended
157211 // value is therefore different each time.
@@ -165,13 +219,13 @@ jQuery.fn.extend({
165219 }
166220
167221 if ( jQuery . isArray ( val ) && rradiocheck . test ( this . type ) ) {
168- this . checked = jQuery . inArray ( jQuery ( this ) . val ( ) , val ) >= 0 ;
222+ this . checked = jQuery . inArray ( self . val ( ) , val ) >= 0 ;
169223
170224 } else if ( jQuery . nodeName ( this , "select" ) ) {
171225 var values = jQuery . makeArray ( val ) ;
172226
173227 jQuery ( "option" , this ) . each ( function ( ) {
174- this . selected = jQuery . inArray ( jQuery ( this ) . val ( ) , values ) >= 0 ;
228+ this . selected = jQuery . inArray ( self . val ( ) , values ) >= 0 ;
175229 } ) ;
176230
177231 if ( ! values . length ) {
@@ -185,50 +239,6 @@ jQuery.fn.extend({
185239 }
186240} ) ;
187241
188- jQuery . each ( {
189- removeAttr : function ( name ) {
190- jQuery . attr ( this , name , "" ) ;
191- if ( this . nodeType === 1 ) {
192- this . removeAttribute ( name ) ;
193- }
194- } ,
195-
196- toggleClass : function ( classNames , state ) {
197- var type = typeof classNames ;
198-
199- if ( type === "string" ) {
200- // toggle individual class names
201- var isBool = typeof state === "boolean" , className , i = 0 ,
202- classNames = classNames . split ( rspace ) ;
203-
204- while ( ( className = classNames [ i ++ ] ) ) {
205- // check each className given, space seperated list
206- state = isBool ? state : ! jQuery ( this ) . hasClass ( className ) ;
207- jQuery ( this ) [ state ? "addClass" : "removeClass" ] ( className ) ;
208- }
209-
210- } else if ( type === "undefined" || type === "boolean" ) {
211- if ( this . className ) {
212- // store className if set
213- jQuery . data ( this , "__className__" , this . className ) ;
214- }
215-
216- // toggle whole className
217- this . className = this . className || classNames === false ? "" : jQuery . data ( this , "__className__" ) || "" ;
218- }
219- }
220- } , function ( name , fn ) {
221- jQuery . fn [ name ] = function ( val , state ) {
222- if ( jQuery . isFunction ( val ) ) {
223- return this . each ( function ( ) {
224- jQuery ( this ) [ name ] ( val . call ( this ) , state ) ;
225- } ) ;
226- }
227-
228- return this . each ( fn , arguments ) ;
229- } ;
230- } ) ;
231-
232242jQuery . extend ( {
233243 attrFn : {
234244 val : true ,
0 commit comments