解决 JavaScript 中 Cannot read properties of null (reading 'setAttribute') 错误
“Cannot read properties of null (reading 'setAttribute')”错误的发生有两个原因:
-
在不存在的 DOM 元素上调用
setAttribute()
方法。 - 在声明 DOM 元素的 HTML 上方插入 JS 脚本标记。
下面是一个产生上述错误的示例
const el = document.getElementById('does-not-exist');
console.log(el); // 👉️ null
// ⛔️ Uncaught TypeError: Cannot read properties of null (reading 'setAttribute')
el.setAttribute('style', 'color: green');
我们试图对导致错误的空值调用 setAttribute
方法。
在调用 setAttribute 之前确保 DOM 元素存在
要解决“Cannot read properties of null (reading 'setAttribute')”错误,需要确保我们用于获取 DOM 元素的 ID 有效。
该错误通常发生在使用无效 ID 调用 getElementById
方法时。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<!-- ✅ `id` should match with JS code -->
<div id="box"></div>
<script src="index.js"></script>
</body>
</html>
在 HTML 代码中为元素设置的 ID 应与我们在调用 document.getElementById()
方法时传递的 ID 相匹配。
将 JS 脚本标签放在 body 标签的底部
要解决“Cannot read properties of null (reading 'setAttribute')
”错误,需要确保在声明 DOM 元素后将 JS 脚本标记放在 body 标记的底部。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<!-- ❌ BAD - script is run before div is created ❌ -->
<script src="index.js"></script>
<div id="box"></div>
</body>
</html>
请注意
,JS 脚本标记位于 div 元素上方。
因此 index.js 文件在创建 HTML 元素之前运行,因此我们无法访问 index.js 文件内的 div。
const el = document.getElementById('box');
console.log(el); // 👉️ null
// ⛔️ Cannot read properties of null (reading 'setAttribute')
el.setAttribute('style', 'color: green');
在创建 DOM 元素后,我们应该在正文底部插入 JS 脚本标记。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<div id="box"></div>
<!-- ✅ GOOD - div is already created ✅ -->
<script src="index.js"></script>
</body>
</html>
现在可以在 index.js 文件中访问 div
元素。
const el = document.getElementById('box');
console.log(el); // 👉️ div#box
// ✅ works
el.setAttribute('style', 'color: green');
HTML 代码是从上到下解析的,因此 script
标签必须放在 body
标签的底部,在它需要访问的所有 DOM 元素之后。
总结
尝试对空值调用 setAttribute
方法时,会出现“Cannot read properties of null (reading 'setAttribute')”错误。
要解决该错误,需要确保为 getElementById
方法提供正确的 ID,并在创建 DOM 元素后加载 JS 脚本。
相关文章
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 中合并两个数组,以及如何删除任何重复的数组。