Multiple keys with the same name are not valid and will cause an error in strict mode.
Create a function / plugin that applies the properties of your cssObj
. If a string string string is found, set the CSS property with the desired value.
If an array is found, skip it and update the property with each value. If an invalid value is found, it is ignored.
DEMO: http://jsfiddle.net/RgfQw/
// Created a plugin for project portability (function($){ $.fn.cssMap = function(map){ var $element = this; $.each(map, function(property, value){ if (value instanceof Array) { for(var i=0, len=value.length; i<len; i++) { $element.css(property, value[i]); } } else { $element.css(property, value); } }); } })(jQuery); // Usage: var cssObj = { 'background-color': '#000', 'background-image': ['-webkit-linear-gradient(top,#000,#fff)', 'linear-gradient(top,#000,#fff)'] }; $(".element").cssMap(cssObj);
Rob w
source share