繁体   English   中英

总是出现“无法读取null的属性'style'的错误”并且我不明白为什么

[英]Always getting “Cannot read property 'style' of null” error and I dont see why

我的.html文件

  <span id="menu_start_random">Random</span>
  <span id="menu_start_prop">Custom</span>

我的.js文件

function menuToggle(action, id) {
  switch (action) {
    case true:
      document.getElementById(id).style.display = 'block';
      break;
    case false:
      document.getElementById(id).style.display = 'none';
      break;
  }
}

function doIt(mode)
{
  switch (mode) {
    case "random":
      menuToggle(false, "menu_start_custom");
      break;

    case "custom":
      menuToggle(false, "menu_start_prop");
      break;
  }
}

现在,当我调用函数doIt("random") ,由于任何原因,我总是收到错误“无法读取null的'style'属性”,但是doIt("custom")按计划工作。 有人可以帮我吗?

因为您没有menu_start_custom元素。

 function menuToggle(action, id) { switch (action) { case true: document.getElementById(id).style.display = 'block'; break; case false: document.getElementById(id).style.display = 'none'; break; } } function doIt(mode) { switch (mode) { case "random": menuToggle(false, "menu_start_custom"); break; case "custom": menuToggle(false, "menu_start_prop"); break; } } 
 <span id="menu_start_custom">Random</span> <span id="menu_start_prop">Custom</span> 

在你的HTML

<span id="menu_start_random">Random</span>

在你的js里

menuToggle(false, "menu_start_custom");

所以menu_start_custom不存在

暂无
暂无

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

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