<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
给我你的怀抱2017-05-19 10:28:55
There are 2 problems:
1.main is missing a double quote
2. There is a duplicate id
高洛峰2017-05-19 10:28:55
Because your class="main"
is missing the final double quote, there is a problem with browser rendering. . .
给我你的怀抱2017-05-19 10:28:55
Your double quotes added your first p tag. That’s why the array length that appears is 3.
过去多啦不再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