如何使用 jQuery 获取所有元素 css 属性的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1471118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get list of all element css attributes with jQuery?
提问by newbie
I need to get list of elements all css attributes. How can I do that ?
我需要获取所有 css 属性的元素列表。我怎样才能做到这一点 ?
回答by gnarf
Copying the source from SO1004475 - jQuery CSS plugin that returns computed style of element to pseudo clone that element?- Please follow link and upvote there if you find it useful.
从SO1004475复制源- jQuery CSS 插件返回元素的计算样式以伪克隆该元素?- 如果您觉得有用,请点击链接并在那里点赞。
It seems ridiculous, but this is probably your best option - makes .css()
with no arguments get an object with all this stuff set.
这看起来很荒谬,但这可能是你最好的选择——让.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);
Note that this doesn't capture all possible CSS properties, particularly new ones for CSS3. Here is a list of all standard CSS and stable CSS3 properties, and here's one of hyphen-prefixed vendor-specific properties(such as -moz-border-start
). If you really want all of them, you can glean them from there.
请注意,这不会捕获所有可能的 CSS 属性,尤其是 CSS3 的新属性。这是一个所有标准的CSS和稳定的CSS3属性列表,这里是一个连字符为前缀的私有属性(如-moz-border-start
)。如果你真的想要所有这些,你可以从那里收集它们。
回答by Mike Sherov
As of jQuery 1.8, you can do:
从 jQuery 1.8 开始,您可以执行以下操作:
$( "#yourElement" ).css( "cssText" );
Which uses the underlying window.getComputedStyle( element ).cssText
. That's the simplest way.
其中使用底层window.getComputedStyle( element ).cssText
. 这是最简单的方法。
回答by ScottE
For inline styles:
对于内联样式:
var styles = $("#someelement").attr("style");
From there, you should be able to split this string if you need to loop the styles.
从那里,如果您需要循环样式,您应该能够拆分此字符串。
To check individual styles, check the docs:
要检查单个样式,请查看文档:
回答by rahul
$("#div1).css("background-color");
will return the background color property of an element with id div1.
将返回 id 为 div1 的元素的背景颜色属性。
Sorry, I don't know a way to get all the CSS attributes using jQuery.
抱歉,我不知道使用 jQuery 获取所有 CSS 属性的方法。