从SO1004475 - jQuery CSS plugin that returns computed style of element to pseudo clone that element?复制源代码 - 如果您觉得有用,请点击链接并在那里投票。
这看起来很荒谬,但这可能是你最好的选择 - 让没有参数的.css() 得到一个包含所有这些东西的对象。
jQuery.fn.css = (function(css2) {
return function() {
if (arguments.length) { return css2.apply(this, arguments); }
var attr = ['font-family','font-size','font-weight','font-style','color',
'text-transform','text-decoration','letter-spacing','word-spacing',
'line-height','text-align','vertical-align','direction','background-color',
'background-image','background-repeat','background-position',
'background-attachment','opacity','width','height','top','right','bottom',
'left','margin-top','margin-right','margin-bottom','margin-left',
'padding-top','padding-right','padding-bottom','padding-left',
'border-top-width','border-right-width','border-bottom-width',
'border-left-width','border-top-color','border-right-color',
'border-bottom-color','border-left-color','border-top-style',
'border-right-style','border-bottom-style','border-left-style','position',
'display','visibility','z-index','overflow-x','overflow-y','white-space',
'clip','float','clear','cursor','list-style-image','list-style-position',
'list-style-type','marker-offset'];
var len = attr.length, obj = {};
for (var i = 0; i < len; i++) {
obj[attr[i]] = css2.call(this, attr[i]);
}
return obj;
};
})(jQuery.fn.css);
请注意,这并不能捕获所有可能的 CSS 属性,尤其是 CSS3 的新属性。这是list of all standard CSS and stable CSS3 properties,这是hyphen-prefixed vendor-specific properties 之一(例如-moz-border-start)。如果你真的想要所有这些,你可以从那里收集它们。