search

Home  >  Q&A  >  body text

javascript - js gets page elements

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <p class="main>
            <p class="item" id="14">24</p>    
            <p class="item" id="12">22</p>
            <p class="item" id="13">2</p>    
            <p class="item" id="14">2</p>    
        </p>
    </body>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript">
        var arr = document.getElementsByClassName('item')
        console.log(arr.length )
        $('.item').each(function(index) {
            alert(index + ': ' + $(this).text());
          });
    </script>
</html>

Why the printed result is 3, the first one is 22, it is correct after removing the main tag, please ask for the reason

大家讲道理大家讲道理2813 days ago571

reply all(6)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:28:55

    There are 2 problems:
    1.main is missing a double quote
    2. There is a duplicate id

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:28:55

    <p class="main> You left a double quote

    reply
    0
  • 高洛峰

    高洛峰2017-05-19 10:28:55

    Because your class="main" is missing the final double quote, there is a problem with browser rendering. . .

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:28:55

    Your double quotes added your first p tag. That’s why the array length that appears is 3.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-19 10:28:55

    Didn’t you notice something unusual about the first P tag? . . , the light pink exudes a unique atmosphere~, the main is missing the closing quotation marks

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:28:55

    main is missing a double quote. . .

    reply
    0
  • Cancelreply