JavaScript 中 Cannot read property 'querySelectorAll' of Null 错误
“Cannot read property 'querySelectorAll' of Null”错误的发生有两个原因:
-
对空值(不存在的 DOM 元素)调用
querySelectorAll()
方法。 - 将 JS 脚本标记放在声明 DOM 元素的 HTML 上方。
下面是产生上述错误的示例代码。
const el = null;
// ⛔️ Uncaught TypeError: Cannot read properties of null (reading 'querySelectorAll')
el.querySelectorAll('div.box');
要解决“Cannot read property 'querySelectorAll' of null”错误,请确保您用于获取元素的 ID 存在于 DOM 中。 该错误通常在向 getElementById
方法提供无效 ID 后发生。
const el = document.getElementById('does-not-exist');
console.log(el); // 👉️ null
// ⛔️ Cannot read properties of null (reading 'querySelectorAll')
el.querySelectorAll('div.box');
我们将一个不存在的 id 传递给 getElementById
方法并得到一个空值。 对空值调用 querySelectorAll()
方法会导致错误。
要解决“Cannot read property 'querySelectorAll' of null”错误,请将 JS 脚本标签放在 body 标签的底部。 该脚本应在创建 DOM 元素后运行。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<!-- ⛔️ BAD - script runs before div exists ⛔️ -->
<script src="index.js"></script>
<div id="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
</div>
</body>
</html>
请注意
,JS 脚本标记位于声明 div 元素的代码上方。 这意味着 index.js 文件无法访问 div 元素。
const el = document.getElementById('container');
console.log(el); // 👉️ null
// ⛔️ Cannot read properties of null (reading 'querySelectorAll')
el.querySelectorAll('div.box');
相反,我们应该将 JS 脚本标记移动到正文的底部,在它尝试访问的元素之后。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<div id="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
</div>
<!-- ✅ GOOD - div already exists ✅ -->
<script src="index.js"></script>
</body>
</html>
现在我们的 index.js 文件可以访问 DOM 元素。
const el = document.getElementById('container');
console.log(el); // 👉️ div#container
// ✅ Works
const boxes = el.querySelectorAll('div.box');
console.log(boxes); // 👉️ [div.box, div.box]
总结
尝试对 null 值调用 querySelectorAll
方法时发生“Cannot read property 'querySelectorAll' of Null”错误。
要解决该错误,需要在 DOM 元素可用后运行 JS 脚本,并确保仅在有效的 DOM 元素上调用该方法。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。