HTML5 Javascript Selector 測試

20201210

by wllai2001

HTML

<h2 class="example">A heading with class="example"</h2>
<p class="example">A paragraph with class="example".</p> 

<p>Click the button to add a background color all elements with class="example".</p>

<button onclick="myFunction()">Try it</button>

<p><strong>Note:</strong> The querySelectorAll() method is not supported in Internet Explorer 8 and earlier versions.</p>

<script>
function myFunction() {
  var x, y, z;
  z = document.querySelectorAll("p").forEach(element => console.log(element.outerHTML));
  y = document.querySelectorAll("p").forEach(element => element.style.backgroundColor = "green");
  x = document.querySelectorAll(".example").forEach(element => element.style.backgroundColor = "red"); 
}
</script>