javascript - js gets page elements
大家讲道理
大家讲道理 2017-05-19 10:26:55
0
6
575
<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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(6)
给我你的怀抱

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

伊谢尔伦

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

小葫芦

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

给我你的怀抱

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

过去多啦不再A梦

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

为情所困

main is missing a double quote. . .

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