1 // exclude the following css properties to add px
2 var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
4 defaultView = document.defaultView || {};
7 attr: function( name, value, type ) {
8 var options = name, isFunction = jQuery.isFunction( value );
10 // Look for the case where we're accessing a style value
11 if ( typeof name === "string" ) {
12 if ( value === undefined ) {
14 jQuery[ type || "attr" ]( this[0], name ) :
19 options[ name ] = value;
23 // Check to see if we're setting style values
24 for ( var i = 0, l = this.length; i < l; i++ ) {
28 for ( var prop in options ) {
29 value = options[prop];
32 value = value.call( elem, i );
35 if ( typeof value === "number" && type === "curCSS" && !exclude.test(prop) ) {
39 jQuery.attr( type ? elem.style : elem, prop, value );
46 css: function( key, value ) {
47 // ignore negative width and height values
48 if ( (key == 'width' || key == 'height') && parseFloat(value) < 0 )
50 return this.attr( key, value, "curCSS" );
53 hasClass: function( selector ) {
54 return !!selector && this.is( "." + selector );
57 val: function( value ) {
58 if ( value === undefined ) {
62 if( jQuery.nodeName( elem, 'option' ) )
63 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
65 // We need to handle select boxes special
66 if ( jQuery.nodeName( elem, "select" ) ) {
67 var index = elem.selectedIndex,
69 options = elem.options,
70 one = elem.type == "select-one";
72 // Nothing was selected
76 // Loop through all the selected options
77 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
78 var option = options[ i ];
80 if ( option.selected ) {
81 // Get the specifc value for the option
82 value = jQuery(option).val();
84 // We don't need an array for one selects
88 // Multi-Selects return an array
96 // Everything else, we just grab the value
97 return (elem.value || "").replace(/\r/g, "");
104 if ( typeof value === "number" )
107 return this.each(function(){
108 if ( this.nodeType != 1 )
111 if ( jQuery.isArray(value) && /radio|checkbox/.test( this.type ) )
112 this.checked = (jQuery.inArray(this.value, value) >= 0 ||
113 jQuery.inArray(this.name, value) >= 0);
115 else if ( jQuery.nodeName( this, "select" ) ) {
116 var values = jQuery.makeArray(value);
118 jQuery( "option", this ).each(function(){
119 this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
120 jQuery.inArray( this.text, values ) >= 0);
123 if ( !values.length )
124 this.selectedIndex = -1;
133 removeAttr: function( name ) {
134 jQuery.attr( this, name, "" );
135 if (this.nodeType == 1)
136 this.removeAttribute( name );
139 addClass: function( classNames ) {
140 jQuery.className.add( this, classNames );
143 removeClass: function( classNames ) {
144 jQuery.className.remove( this, classNames );
147 toggleClass: function( classNames, state ) {
148 if( typeof state !== "boolean" )
149 state = !jQuery.className.has( this, classNames );
150 jQuery.className[ state ? "add" : "remove" ]( this, classNames );
152 }, function(name, fn){
153 jQuery.fn[ name ] = function(){
154 return this.each( fn, arguments );
160 // internal only, use addClass("class")
161 add: function( elem, classNames ) {
162 jQuery.each((classNames || "").split(/\s+/), function(i, className){
163 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
164 elem.className += (elem.className ? " " : "") + className;
168 // internal only, use removeClass("class")
169 remove: function( elem, classNames ) {
170 if (elem.nodeType == 1)
171 elem.className = classNames !== undefined ?
172 jQuery.grep(elem.className.split(/\s+/), function(className){
173 return !jQuery.className.has( classNames, className );
178 // internal only, use hasClass("class")
179 has: function( elem, className ) {
180 return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
184 // A method for quickly swapping in/out CSS properties to get correct calculations
185 swap: function( elem, options, callback ) {
187 // Remember the old values, and insert the new ones
188 for ( var name in options ) {
189 old[ name ] = elem.style[ name ];
190 elem.style[ name ] = options[ name ];
193 callback.call( elem );
195 // Revert the old values
196 for ( var name in options )
197 elem.style[ name ] = old[ name ];
200 css: function( elem, name, force, extra ) {
201 if ( name == "width" || name == "height" ) {
202 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
205 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
207 if ( extra === "border" )
210 jQuery.each( which, function() {
212 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
213 if ( extra === "margin" )
214 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
216 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
220 if ( elem.offsetWidth !== 0 )
223 jQuery.swap( elem, props, getWH );
225 return Math.max(0, Math.round(val));
228 return jQuery.curCSS( elem, name, force );
231 curCSS: function( elem, name, force ) {
232 var ret, style = elem.style;
234 // We need to handle opacity special in IE
235 if ( name == "opacity" && !jQuery.support.opacity ) {
236 ret = jQuery.attr( style, "opacity" );
243 // Make sure we're using the right name for getting the float value
244 if ( name.match( /float/i ) )
247 if ( !force && style && style[ name ] )
250 else if ( defaultView.getComputedStyle ) {
252 // Only "float" is needed here
253 if ( name.match( /float/i ) )
256 name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
258 var computedStyle = defaultView.getComputedStyle( elem, null );
261 ret = computedStyle.getPropertyValue( name );
263 // We should always get a number back from opacity
264 if ( name == "opacity" && ret == "" )
267 } else if ( elem.currentStyle ) {
268 var camelCase = name.replace(/\-(\w)/g, function(all, letter){
269 return letter.toUpperCase();
272 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
274 // From the awesome hack by Dean Edwards
275 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
277 // If we're not dealing with a regular pixel number
278 // but a number that has a weird ending, we need to convert it to pixels
279 if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
280 // Remember the original values
281 var left = style.left, rsLeft = elem.runtimeStyle.left;
283 // Put in the new values to get a computed value out
284 elem.runtimeStyle.left = elem.currentStyle.left;
285 style.left = ret || 0;
286 ret = style.pixelLeft + "px";
288 // Revert the changed values
290 elem.runtimeStyle.left = rsLeft;
297 attr: function( elem, name, value ) {
298 // don't set attributes on text and comment nodes
299 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
302 var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ),
303 // Whether we are setting (or getting)
304 set = value !== undefined;
306 // Try to normalize/fix the name
307 name = notxml && jQuery.props[ name ] || name;
309 // Only do all the following if this is a node (faster for style)
310 // IE elem.getAttribute passes even for style
311 if ( elem.tagName ) {
313 // These attributes require special treatment
314 var special = /href|src|style/.test( name );
316 // Safari mis-reports the default selected property of a hidden option
317 // Accessing the parent's selectedIndex property fixes it
318 if ( name == "selected" && elem.parentNode )
319 elem.parentNode.selectedIndex;
321 // If applicable, access the attribute via the DOM 0 way
322 if ( name in elem && notxml && !special ) {
324 // We can't allow the type property to be changed (since it causes problems in IE)
325 if ( name == "type" && elem.nodeName.match(/(button|input)/i) && elem.parentNode )
326 throw "type property can't be changed";
328 elem[ name ] = value;
331 // browsers index elements by id/name on forms, give priority to attributes.
332 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
333 return elem.getAttributeNode( name ).nodeValue;
335 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
336 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
337 if ( name == "tabIndex" ) {
338 var attributeNode = elem.getAttributeNode( "tabIndex" );
339 return attributeNode && attributeNode.specified
340 ? attributeNode.value
341 : elem.nodeName.match(/(button|input|object|select|textarea)/i)
343 : elem.nodeName.match(/^(a|area)$/i) && elem.href
351 if ( !jQuery.support.style && notxml && name == "style" )
352 return jQuery.attr( elem.style, "cssText", value );
355 // convert the value to a string (all browsers do this but IE) see #1070
356 elem.setAttribute( name, "" + value );
358 var attr = !jQuery.support.hrefNormalized && notxml && special
359 // Some attributes require a special call on IE
360 ? elem.getAttribute( name, 2 )
361 : elem.getAttribute( name );
363 // Non-existent attributes return null, we normalize to undefined
364 return attr === null ? undefined : attr;
367 // elem is actually elem.style ... set the style
369 // IE uses filters for opacity
370 if ( !jQuery.support.opacity && name == "opacity" ) {
372 // IE has trouble with opacity if it does not have layout
373 // Force it by setting the zoom level
376 // Set the alpha filter to set the opacity
377 elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
378 (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
381 return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
382 (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
386 name = name.replace(/-([a-z])/ig, function(all, letter){
387 return letter.toUpperCase();
391 elem[ name ] = value;