代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1 class="title">hello</h1> <h2 class="title">HELLO</h2> <script> // 真数组 Array let array = ['张三', '李四', '王五'] array.push('老刘') console.log(array) // 伪数组 非Array let titles = document.querySelectorAll('.title') console.log(titles) // 伪数组也有下标,也有长度,可以通过for循环遍历 for(let index = 0; index < titles.length; index++){ console.log(titles[index]) } // 报错,伪数组不能调用真数组的方法 titles.push('123') </script> </body> </html>
效果图:
web API-DOM-真数组和伪数组
最新推荐文章于 2024-08-15 18:19:19 发布