所以我的代码中有一个错误。错误信息是:
Uncaught TypeError: Cannot set property 'className' of null Error
这是代码:
HTML:
<section id="section-14">
<div class="mediabox">
<img src="product/Diffusers/Diffusers_1.jpg" alt="Diffusers">
<h3>Diffusers</h3>
</div>
</section>
Javascript:
document.getElementById('section-14').className = "content-current";
请有人指导我纠正我的错误。
最佳答案
将脚本添加到正文标记末尾之前。你的代码应该像这样 -
<html>
<head>
</head>
<body>
<section id="section-14">
<div class="mediabox">
<img src="product/Diffusers/Diffusers_1.jpg" alt="Diffusers">
<h3>Diffusers</h3>
</div>
</section>
<script>
document.getElementById("section-14").className="content-current";
</script>
</body>
</html>
希望这对您有帮助。
关于javascript - 未捕获的类型错误 : Cannot set property 'className' of null Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34462987/