Use parent selector in CSS to query elements in JavaScript

Description

The following code shows how to use parent selector in CSS to query elements.

Example


<!--   w  w w.j av a 2s . c  o  m-->
<!DOCTYPE HTML>
<html>
<body>
<pre id="results"></pre>
<p id="tblock">
This is a HTML document.
<span id="banana">b</span>
<span id="apple">a</span>,
<span="orange">C</span="orange">
</p>
<script>

var selElems = document.querySelectorAll("#tblock > span");
document.writeln(selElems.length);
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use parent selector in CSS to query elements in JavaScript
Home »
  Javascript Tutorial »
    Document »
      Operation
Javascript Tutorial Operation
Append content to the end of the HTML docum...
Change the document title in JavaScript
Check if the document object has the queryS...
Delete all cookies by setting the expire da...
Navigate to the new URL and make an entry i...
Query HTML document using CSS selector in J...
Set new hash for a page in JavaScript
Use document.writeln() to append tags in Ja...
Use parent selector in CSS to query element...