2 attr: function( name, value, type ) {
3 var options = name, isFunction = jQuery.isFunction( value );
5 // Look for the case where we're accessing a style value
6 if ( typeof name === "string" ) {
7 if ( value === undefined ) {
9 jQuery[ type || "attr" ]( this[0], name ) :
14 options[ name ] = value;
18 // Check to see if we're setting style values
19 for ( var i = 0, l = this.length; i < l; i++ ) {
23 for ( var prop in options ) {
24 value = options[prop];
27 value = value.call( elem, i );
30 if ( typeof value === "number" && type === "curCSS" && !exclude.test(prop) ) {
34 jQuery.attr( type ? elem.style : elem, prop, value );
41 css: function( key, value ) {
42 // ignore negative width and height values
43 if ( (key == 'width' || key == 'height') && parseFloat(value) < 0 )
45 return this.attr( key, value, "curCSS" );
48 hasClass: function( selector ) {
49 return !!selector && this.is( "." + selector );
52 val: function( value ) {
53 if ( value === undefined ) {
57 if( jQuery.nodeName( elem, 'option' ) )
58 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
60 // We need to handle select boxes special
61 if ( jQuery.nodeName( elem, "select" ) ) {
62 var index = elem.selectedIndex,
64 options = elem.options,
65 one = elem.type == "select-one";
67 // Nothing was selected
71 // Loop through all the selected options
72 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
73 var option = options[ i ];
75 if ( option.selected ) {
76 // Get the specifc value for the option
77 value = jQuery(option).val();
79 // We don't need an array for one selects
83 // Multi-Selects return an array
91 // Everything else, we just grab the value
92 return (elem.value || "").replace(/\r/g, "");
99 if ( typeof value === "number" )
102 return this.each(function(){
103 if ( this.nodeType != 1 )
106 if ( jQuery.isArray(value) && /radio|checkbox/.test( this.type ) )
107 this.checked = (jQuery.inArray(this.value, value) >= 0 ||
108 jQuery.inArray(this.name, value) >= 0);
110 else if ( jQuery.nodeName( this, "select" ) ) {
111 var values = jQuery.makeArray(value);
113 jQuery( "option", this ).each(function(){
114 this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
115 jQuery.inArray( this.text, values ) >= 0);
118 if ( !values.length )
119 this.selectedIndex = -1;
128 removeAttr: function( name ) {
129 jQuery.attr( this, name, "" );
130 if (this.nodeType == 1)
131 this.removeAttribute( name );
134 addClass: function( classNames ) {
135 jQuery.className.add( this, classNames );
138 removeClass: function( classNames ) {
139 jQuery.className.remove( this, classNames );
142 toggleClass: function( classNames, state ) {
143 if( typeof state !== "boolean" )
144 state = !jQuery.className.has( this, classNames );
145 jQuery.className[ state ? "add" : "remove" ]( this, classNames );
147 }, function(name, fn){
148 jQuery.fn[ name ] = function(){
149 return this.each( fn, arguments );
155 // internal only, use addClass("class")
156 add: function( elem, classNames ) {
157 jQuery.each((classNames || "").split(/\s+/), function(i, className){
158 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
159 elem.className += (elem.className ? " " : "") + className;
163 // internal only, use removeClass("class")
164 remove: function( elem, classNames ) {
165 if (elem.nodeType == 1)
166 elem.className = classNames !== undefined ?
167 jQuery.grep(elem.className.split(/\s+/), function(className){
168 return !jQuery.className.has( classNames, className );
173 // internal only, use hasClass("class")
174 has: function( elem, className ) {
175 return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
179 // A method for quickly swapping in/out CSS properties to get correct calculations
180 swap: function( elem, options, callback ) {
182 // Remember the old values, and insert the new ones
183 for ( var name in options ) {
184 old[ name ] = elem.style[ name ];
185 elem.style[ name ] = options[ name ];
188 callback.call( elem );
190 // Revert the old values
191 for ( var name in options )
192 elem.style[ name ] = old[ name ];
195 css: function( elem, name, force, extra ) {
196 if ( name == "width" || name == "height" ) {
197 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
200 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
202 if ( extra === "border" )
205 jQuery.each( which, function() {
207 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
208 if ( extra === "margin" )
209 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
211 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
215 if ( elem.offsetWidth !== 0 )
218 jQuery.swap( elem, props, getWH );
220 return Math.max(0, Math.round(val));
223 return jQuery.curCSS( elem, name, force );
226 curCSS: function( elem, name, force ) {
227 var ret, style = elem.style;
229 // We need to handle opacity special in IE
230 if ( name == "opacity" && !jQuery.support.opacity ) {
231 ret = jQuery.attr( style, "opacity" );
238 // Make sure we're using the right name for getting the float value
239 if ( name.match( /float/i ) )
242 if ( !force && style && style[ name ] )
245 else if ( defaultView.getComputedStyle ) {
247 // Only "float" is needed here
248 if ( name.match( /float/i ) )
251 name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
253 var computedStyle = defaultView.getComputedStyle( elem, null );
256 ret = computedStyle.getPropertyValue( name );
258 // We should always get a number back from opacity
259 if ( name == "opacity" && ret == "" )
262 } else if ( elem.currentStyle ) {
263 var camelCase = name.replace(/\-(\w)/g, function(all, letter){
264 return letter.toUpperCase();
267 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
269 // From the awesome hack by Dean Edwards
270 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
272 // If we're not dealing with a regular pixel number
273 // but a number that has a weird ending, we need to convert it to pixels
274 if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
275 // Remember the original values
276 var left = style.left, rsLeft = elem.runtimeStyle.left;
278 // Put in the new values to get a computed value out
279 elem.runtimeStyle.left = elem.currentStyle.left;
280 style.left = ret || 0;
281 ret = style.pixelLeft + "px";
283 // Revert the changed values
285 elem.runtimeStyle.left = rsLeft;
292 attr: function( elem, name, value ) {
293 // don't set attributes on text and comment nodes
294 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
297 var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ),
298 // Whether we are setting (or getting)
299 set = value !== undefined;
301 // Try to normalize/fix the name
302 name = notxml && jQuery.props[ name ] || name;
304 // Only do all the following if this is a node (faster for style)
305 // IE elem.getAttribute passes even for style
306 if ( elem.tagName ) {
308 // These attributes require special treatment
309 var special = /href|src|style/.test( name );
311 // Safari mis-reports the default selected property of a hidden option
312 // Accessing the parent's selectedIndex property fixes it
313 if ( name == "selected" && elem.parentNode )
314 elem.parentNode.selectedIndex;
316 // If applicable, access the attribute via the DOM 0 way
317 if ( name in elem && notxml && !special ) {
319 // We can't allow the type property to be changed (since it causes problems in IE)
320 if ( name == "type" && elem.nodeName.match(/(button|input)/i) && elem.parentNode )
321 throw "type property can't be changed";
323 elem[ name ] = value;
326 // browsers index elements by id/name on forms, give priority to attributes.
327 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
328 return elem.getAttributeNode( name ).nodeValue;
330 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
331 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
332 if ( name == "tabIndex" ) {
333 var attributeNode = elem.getAttributeNode( "tabIndex" );
334 return attributeNode && attributeNode.specified
335 ? attributeNode.value
336 : elem.nodeName.match(/(button|input|object|select|textarea)/i)
338 : elem.nodeName.match(/^(a|area)$/i) && elem.href
346 if ( !jQuery.support.style && notxml && name == "style" )
347 return jQuery.attr( elem.style, "cssText", value );
350 // convert the value to a string (all browsers do this but IE) see #1070
351 elem.setAttribute( name, "" + value );
353 var attr = !jQuery.support.hrefNormalized && notxml && special
354 // Some attributes require a special call on IE
355 ? elem.getAttribute( name, 2 )
356 : elem.getAttribute( name );
358 // Non-existent attributes return null, we normalize to undefined
359 return attr === null ? undefined : attr;
362 // elem is actually elem.style ... set the style
364 // IE uses filters for opacity
365 if ( !jQuery.support.opacity && name == "opacity" ) {
367 // IE has trouble with opacity if it does not have layout
368 // Force it by setting the zoom level
371 // Set the alpha filter to set the opacity
372 elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
373 (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
376 return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
377 (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
381 name = name.replace(/-([a-z])/ig, function(all, letter){
382 return letter.toUpperCase();
386 elem[ name ] = value;