未捕获的类型错误:无法读取 null 的属性(读取“appendChild”);持续错误
我的脚本标签位于我的正文标签的底部,这是我在这里找到的主要解决方案。还有其他原因导致appendChild 遇到此错误吗?
HTML:
<section class="section-2">
<p class="about-me-text"></p>
</section>
<script src="rScript.js"></script>
</body>
JavaScript:
const aboutMeText = document.querySelector("about-me-text");
const aboutMeTextContent = 'I am a creative designer, who dabbles in both website
creation & digital art design. Contact me to start a creative project or website
today.'
Array.from(aboutMeTextContent).forEach(char => {
const span = document.createElement("span");
span.textContent = char;
aboutMeText.appendChild(span);
})
My script tag is at the bottom of my body tag which is the main solution I have found on here. Is there any other reasons appendChild would be running into this error?
Html:
<section class="section-2">
<p class="about-me-text"></p>
</section>
<script src="rScript.js"></script>
</body>
Javascript:
const aboutMeText = document.querySelector("about-me-text");
const aboutMeTextContent = 'I am a creative designer, who dabbles in both website
creation & digital art design. Contact me to start a creative project or website
today.'
Array.from(aboutMeTextContent).forEach(char => {
const span = document.createElement("span");
span.textContent = char;
aboutMeText.appendChild(span);
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加一个“。”当定位类名时使用 querySelector()。
You need to add a "." to querySelector() when targeting a class name.
使用
querySelector
时,您必须明确要选择哪个元素,例如,在选择一个类时的问题中,您必须将.
放在about-me-text
的开头。对于 ID,您将在开头添加#
等。When using
querySelector
, you have to clarify which element are you selecting, for example, in your problem as you're selecting a class, you have to put a.
in the start ofabout-me-text
. For a ID, you'll add a#
at the start, etc.