Html 未捕获的类型错误:无法将属性“className”设置为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17337080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot set property 'className' of null
提问by chriz
Following code shows error "Uncaught TypeError: Cannot set property 'className' of null"
以下代码显示错误 "Uncaught TypeError: Cannot set property 'className' of null"
<style>
.deactive { height:250px; }
.active { height:150px; }
</style>
<script>
function click(div) {
document.getElementById('div').className = 'active';
}
</script>
<a href="#" onclick="click(Division);">
<div id="Division" class="deactive">
<!--Division contents-->
</div>
</a>
i am trying pass id of division to js function by on click event of anchor tag.. and i want to change the class name of the division, help me to identify the error..
我正在尝试通过锚标记的点击事件将部门的 id 传递给 js 函数..我想更改部门的类名,帮助我识别错误..
回答by Fabrizio Calderan
That error occurs because in your document you have no elements whose id is "div"
so document.getElementById('div')
is null
.
因为你的文档中,你有id为无元素出现的错误"div"
,从而document.getElementById('div')
为null
。
You need to change here:
您需要在此处更改:
onclick="click('division');"
use quotes around argument, and
在参数周围使用引号,以及
document.getElementById(div).className = 'active';
div
here must not be enclosed in quotes, since you're using the parameter passed when the function has been called
div
这里不能用引号括起来,因为您使用的是调用函数时传递的参数
(as a side note: avoid to call your function click
)
(作为旁注:避免调用您的函数click
)
example fiddle
示例小提琴