Jquery Css
Jquery Css
css()
Categories: CSS | Manipulation > Style Properties
Get the value of a computed style property for the first element in the set of matched
elements or set one or more CSS properties for every matched element.
Contents:
.css( propertyName )
.css( propertyName )
.css( propertyNames )
.css( propertyName, value )
.css( properties )
propertyName
o
Type: String
A CSS property.
version added: 1.9.css( propertyNames )
propertyNames
o
Type: Array
Examples:
Example: Get the background color of a clicked div.
1
2
3
4
5
6
7
8
9
10
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>css demo</title>
<style>
div {
width: 60px;
height: 60px;
margin: 5px;
float: left;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<span id="result"> </span>
<div style="background-color:blue;"></div>
<div style="background-color:rgb(15,99,30);"></div>
<div style="background-color:#123456;"></div>
<div style="background-color:#f11;"></div>
13
<script>
$( "div" ).click(function() {
var color = $( this ).css( "background-color" );
$( "#result" ).html( "That div is <span style='color:" +
color + ";'>" + color + "</span>." );
});
</script>
14
</body>
</html>
11
12
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Demo:
Example: Get the width, height, text color, and background color of a clicked div.
1
2
3
4
5
6
7
8
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>css demo</title>
<style>
div {
height: 50px;
margin: 5px;
padding: 5px;
float: left;
}
#box1 {
width: 50px;
color: yellow;
background-color: blue;
}
#box2 {
width: 80px;
color: rgb(255, 255, 255);
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Demo: