javascript - A small question about classes, please answer?
黄舟
黄舟 2017-06-26 10:55:34
0
7
678

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>
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(7)
学习ing

Shouldn’t it be 3?

曾经蜡笔没有小新

The pop-up is 3, because there are three elements with ClassName as example.

仅有的幸福

Returns an array-like object containing all child elements of the specified class name. When the call occurs on a document object, the entire DOM is searched, including the root node. You can also call the getElementsByClassName() method on any element. It will return all child elements with the specified class name with the current element as the root node.

You use getElementsByClassName() to select all nodes with class names example, and popup 3 is correct

我想大声告诉你

Shouldn’t it be 3

学习ing

3. There are three elements whose ClassName is example.

Ty80

The pop-up answer is 3, because there are three classes named example.

Peter_Zhu

Sorry, I just made a mistake and the one in () is demo

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template