Home  >  Q&A  >  body text

javascript - A small question about classes, please answer?

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>
黄舟黄舟2672 days ago627

reply all(7)I'll reply

  • 学习ing

    学习ing2017-06-26 10:57:34

    Shouldn’t it be 3?

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-26 10:57:34

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

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-26 10:57:34

    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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:57:34

    Shouldn’t it be 3

    reply
    0
  • 学习ing

    学习ing2017-06-26 10:57:34

    3. There are three elements whose ClassName is example.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-26 10:57:34

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-26 10:57:34

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

    reply
    0
  • Cancelreply