728x90
SMALL
#jQuery
#css
.css()
Get the css attribute value of the selected element with .css() or add the style attribute.
grammar 1
.css( propertyName )
Add a style property. for example
$( "h1" ).css( "color", "green" );
adds a style attribute to the h1 element and replaces the text color with green.
<h1 style="color: green;">...</h1>
example 1
Prints the value of the color attribute of the h1 element.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
h1 {color: red;}
</style>
<script src="//code.jquery.com/jquery-3.3.1.js"></script>
<script>
$(document).ready(function() {
var color = $( "h1" ).css( "color" );
$( "p" ).html( "Color is " + color );
});
</script>
</head>
<body>
<h1>Lorem ipsum dolor.</h1>
<p></p>
</body>
</html>
example 2
Set the text color of the h1 element to green.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Methods</title>
<style>
h1 {color: red;}
</style>
<script src="//code.jquery.com/jquery-3.3.1.js"></script>
<script>
$(document).ready(function() {
$( "h1" ).css( "color", "green" );
});
</script>
</head>
<body>
<h1>Lorem ipsum dolor.</h1>
</body>
</html>
TIP.
Use .attr() to get the value of an attribute of an HTML element or to add an attribute.
728x90
LIST