Примеры для jQuery Селектор по идентификатору (“#id”)


Select the element with the id "myDiv" and give it a red border.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>id demo</title>
<style>
div {
width: 90px;
height: 90px;
float: left;
padding: 5px;
margin: 5px;
background-color: #eee;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="notMe"><p>id="notMe"</p></div>
<div id="myDiv">id="myDiv"</div>
<script>
$( "#myDiv" ).css( "border", "3px solid red" );
</script>
</body>
</html>

Демонстрация:

Select the element with the id "myID.entry[1]" and give it a red border. Note how certain characters must be escaped with backslashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>id demo</title>
<style>
div {
width: 300px;
float: left;
padding: 2px;
margin: 3px;
background-color: #eee;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="myID.entry[0]">id="myID.entry[0]"</div>
<div id="myID.entry[1]">id="myID.entry[1]"</div>
<div id="myID.entry[2]">id="myID.entry[2]"</div>
<script>
$( "#myID\\.entry\\[1\\]" ).css( "border", "3px solid red" );
</script>
</body>
</html>

Демонстрация: