未捕获的TypeError:无法将属性'className'设置为null

时间:2023-02-09 20:37:18

Following code shows error "Uncaught TypeError: Cannot set property 'className' of null"

以下代码显示错误“Uncaught TypeError:无法设置属性'className'为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..

我正在尝试通过点击锚标记的事件将除法传递给js函数..我想更改除法的类名,帮我识别错误..

1 个解决方案

#1


8  

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)

(作为旁注:避免调用你的功能点击)

example fiddle

#1


8  

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)

(作为旁注:避免调用你的功能点击)

example fiddle