-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectorsAPIExample01.htm
executable file
·39 lines (31 loc) · 1.1 KB
/
SelectorsAPIExample01.htm
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
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html>
<head>
<title>Selectors API Example</title>
</head>
<body>
<ul>
<li>First item</li>
<li class="selected">Second item</li>
<li>Third item</li>
</ul>
<script type="text/javascript">
if (document.querySelector){
//get the body element
var body = document.querySelector("body");
alert(body.tagName);
//get the element with the ID "myDiv"
var myDiv = document.querySelector("#myDiv");
alert(myDiv);
//get first element with a class of "selected"
var selected = document.querySelector(".selected");
alert(selected.innerHTML);
//get first image with class of "button"
var img = document.body.querySelector("img.button");
alert(img);
} else {
alert("Selectors API not supported in this browser");
}
</script>
</body>
</html>