jQuery 获取元素颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3226302/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:02:58  来源:igfitidea点击:

jQuery Get Elements Color?

jquerycssdomcolors

提问by Tom

I have the follow HTML

我有以下 HTML

<div>This is some <span>special <a href="#">text</a></span> and it's super</div>

And CSS

和 CSS

span {color:#333;}
a {color:#777;}
a:hover {color:#AAA;}

I am wondering what I can use to setup a function that I can extract the color of the <a> and <a>:hover elements?

我想知道我可以用什么来设置一个可以提取 <a> 和 <a>:hover 元素颜色的函数?

Thanks

谢谢

回答by RPM1984

Use the .css()method on the element you want to retrieve.

在要检索的元素上使用.css()方法。

In your example:

在你的例子中:

var theColorIs = $('a').css("color");

Which will return the color in RGB.

这将返回 RGB 中的颜色。

回答by Josiah

To get the color CSS attribute of all the elements you can use JQuery's css() function:

要获取所有元素的颜色 CSS 属性,您可以使用 JQuery 的 css() 函数:

$('a').each(function(index) {
               alert( $(this).css('color') );
             });????

This will iterate through all the anchor elements on the page and tell you the CSS color attribute thereof.

这将遍历页面上的所有锚元素并告诉您其 CSS 颜色属性。