Definition and Usage..
The querySelectorAll() method returns all elements in the document that matches a specified CSS selector(s), as a static NodeList object.
The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.
Tip: You can use the length property of the NodeList object to determine the number of elements that matches the specified selector, then you can loop through all elements and extract the info you want.
Syntax: document.querySelectorAll(CSS selectors)
CODING:The querySelectorAll() method returns all elements in the document that matches a specified CSS selector(s), as a static NodeList object.
The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.
Tip: You can use the length property of the NodeList object to determine the number of elements that matches the specified selector, then you can loop through all elements and extract the info you want.
Syntax: document.querySelectorAll(CSS selectors)
<!DOCTYPE html>
<html>
<body>
<h2 class="example">A heading with class="example"</h2>
<p>A paragraph with not class.</p>
<p class="example">A paragraph with class="example".</p>
<a href="http://cprogramaboutexample.blogspot.in/" >cprogramaboutexample</a>
<div id="SocialMedia">
<a href="https://www.facebook.com" target="_top">facebook.com</a>
<a href="http://www.youtube.com" target="_blank">youtube.com</a>
<div>
<br><br>
<button onclick="exclass()">class="example"</button>
<button onclick="ptag()">P Tag</button>
<button onclick="ptagclass()">P Tag with Class</button>
<button id="x" onclick="count()">How Many Class</button>
<button onclick="sm()">a[target]</button>
<button onclick="green()">Specified Tag</button>
<button onclick="link()">a[target] in div tag</button>
<script>
var x,i;
function exclass() {
x = document.querySelectorAll(".example");
x[0].style.color = "blue";
}
function ptag() {
x = document.querySelectorAll("p");
x[0].style.backgroundColor = "red";
}
function ptagclass() {
x = document.querySelectorAll("p.example");
x[0].style.backgroundColor= "orange";
}
function count() {
x=document.querySelectorAll(".example");
document.getElementById("x").innerHTML =x.length+" Class";
for (i = 0; i < x.length; i++) {
x[i].style.border = "3pt solid gold";
}
}
function sm() {
x = document.querySelectorAll("a[target]");
for (i = 0; i < x.length; i++) {
x[i].style.color = "orange";
}
}
function green() {
x = document.querySelectorAll("h2, p, button");
for (i = 0; i < x.length; i++) {
x[i].style.backgroundColor = "lightgreen";
}
}
function link() {
x = document.getElementById("SocialMedia").querySelectorAll("a[target]");
for (i = 0; i < x.length; i++) {
x[i].style.fontSize = "xx-large";
}
}
</script>
</body>
</html>
<html>
<body>
<h2 class="example">A heading with class="example"</h2>
<p>A paragraph with not class.</p>
<p class="example">A paragraph with class="example".</p>
<a href="http://cprogramaboutexample.blogspot.in/" >cprogramaboutexample</a>
<div id="SocialMedia">
<a href="https://www.facebook.com" target="_top">facebook.com</a>
<a href="http://www.youtube.com" target="_blank">youtube.com</a>
<div>
<br><br>
<button onclick="exclass()">class="example"</button>
<button onclick="ptag()">P Tag</button>
<button onclick="ptagclass()">P Tag with Class</button>
<button id="x" onclick="count()">How Many Class</button>
<button onclick="sm()">a[target]</button>
<button onclick="green()">Specified Tag</button>
<button onclick="link()">a[target] in div tag</button>
<script>
var x,i;
function exclass() {
x = document.querySelectorAll(".example");
x[0].style.color = "blue";
}
function ptag() {
x = document.querySelectorAll("p");
x[0].style.backgroundColor = "red";
}
function ptagclass() {
x = document.querySelectorAll("p.example");
x[0].style.backgroundColor= "orange";
}
function count() {
x=document.querySelectorAll(".example");
document.getElementById("x").innerHTML =x.length+" Class";
for (i = 0; i < x.length; i++) {
x[i].style.border = "3pt solid gold";
}
}
function sm() {
x = document.querySelectorAll("a[target]");
for (i = 0; i < x.length; i++) {
x[i].style.color = "orange";
}
}
function green() {
x = document.querySelectorAll("h2, p, button");
for (i = 0; i < x.length; i++) {
x[i].style.backgroundColor = "lightgreen";
}
}
function link() {
x = document.getElementById("SocialMedia").querySelectorAll("a[target]");
for (i = 0; i < x.length; i++) {
x[i].style.fontSize = "xx-large";
}
}
</script>
</body>
</html>
Comments
Post a Comment