我在业余时间正在编写一个简单的基于文本的游戏。这就是所谓的TARDIS飞行(Doctor Who!),我正在主菜单上工作。
因此,在设置主菜单的后,我使用的功能addMainMenuListeners
通过添加所有事件监听器。
一切正常,直到从子菜单之一返回主菜单为止。然后,我发现按钮不再起作用。设置了之后,我正在打电话,但是即使我这样做了,并且我在控制台中进行了检查,但我没有发现任何事件。
代码:
在我的主要javascript文件中:addEventListener
innerHTML
addMainMenuListeners
innerHTML
function addMainMenuListeners() { if($("start")) $("start").addEventListener("click", startGame); if($("instructions")) $("instructions").addEventListener("click", instructions); if($("settings")) $("settings").addEventListener("click", settings); if($("back")) $("back").addEventListener("click", resetMainMenu); if($("volume")) $("volume").addEventListener("change", function(){saveAndChangeVolume($("volume").value);}); } function instructions() { $("mainmenu").innerHTML = "Instructions
Fly your TARDIS through the time vortex using the console. Make sure that you use the correct materialization codes. Try to keep the time-space coordinates close to the values of the ones given. AND DON'T CREATE PARADOXES, WHATEVER YOU DO!
"; addMainMenuListeners(); return true; } function settings() { $("mainmenu").innerHTML = "Volume
1100
"; addMainMenuListeners(); loadVolume(); return true; } function resetMainMenu() { $("mainmenu").innerHTML = "TARDIS Flight
"; addMainMenuListeners(); return true; }
在我的HTML文件中:
如果您需要任何澄清,只需询问。
编辑:显然,没有人明白我的意思。innerHTML
正如从代码中可以看到的那样,我在执行完之后正在读取事件监听器。我根本看不到要添加的事件,该函数正在触发但未添加事件。
另外,我正在使用自定义$
函数,只是一种return document.getElementById(id)
函数。