简体   繁体   English

Javascript 功能不能正常工作。 无法设置 null 的属性“onclick”

[英]Javascript function doesn't work well. Can't set a property 'onclick' of null

I've the following code :我有以下代码:

var exit = document.getElementById("exit");
exit.onclick = function() {
   "use strict";
    document.getElementById("fadedDiv").style.display = "none" ; 
};

But in the console I get the following error:但在控制台中,我收到以下错误:

script.js:107 Uncaught TypeError: Cannot set property 'onclick' of null script.js:107 未捕获的类型错误:无法设置 null 的属性“onclick”

Although it worked well with me before, but I don't know what the problem is.虽然之前对我来说效果很好,但我不知道是什么问题。

plz help me请帮助我

Because in your HTML you gave the parent Div a class name of "fadedDiv", then you try to use因为在你的 HTML 中你给了父 Div 一个“fadedDiv”的类名,然后你尝试使用

document.getElementById("fadedDiv")

You can change the parent div tag to be an id您可以将父 div 标签更改为 id

 <div id="fadedDiv"> 
      this is div
      <div id="exit" >&#10005; text</div> 
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM