The browser pop-up is always different from the one in the book. I don’t understand. Logically speaking, it should be 1. Why did it show 0 when I tried it?
code show as below:
<!DOCTYPE html>
<html>
<body>
<p class="example">123</p>
<p class="example">123</p>
<p class="example">123</p>
<p>456</p>
<p id="demo"></p>
<script>
alert(document.getElementsByClassName("example").length);
</script>
</body>
</html>
Shouldn’t it be 3?
The pop-up is 3, because there are three elements with ClassName as example.
You use
getElementsByClassName()
to select all nodes with class namesexample
, and popup 3 is correctShouldn’t it be 3
3. There are three elements whose ClassName is example.
The pop-up answer is 3, because there are three classes named example.
Sorry, I just made a mistake and the one in () is demo