我的.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")
会按计划工作。有人可以帮我吗?
答案 0 :(得分:1)
因为您没有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>
答案 1 :(得分:1)
在您的html
<span id="menu_start_random">Random</span>
在您的js中
menuToggle(false, "menu_start_custom");
因此menu_start_custom不存在