I have the follow HTML
<div>This is some <span>special <a href="#">text</a></span> and it's super</div>
And 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?
Thanks
Use the . css() method on the element you want to retrieve. In your example: var theColorIs = $('a').
click(function() { var color = $( this ). css( "background-color" ); $( "p" ). html( "That div is " + color + "." ); });
jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.
The css() method in JQuery is used to change the style property of the selected element. The css() in JQuery can be used in different ways. Return value: It will return the value of the property for the selected element.
Use the .css() method on the element you want to retrieve.
In your example:
var theColorIs = $('a').css("color");
Which will return the color in RGB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With