Javascript jquery.show()或.getElementById()存在问题。不确定问题在哪里或如何解决
我正在构建一个选择你自己的冒险游戏(你可能已经看到了我之前关于我遇到的另一个问题的问题)。当用户按下空格键时,我最终将主页隐藏起来,但下一个html元素(播放页面)不会显示。我不确定问题是否出在最初的Javascript jquery.show()或.getElementById()存在问题。不确定问题在哪里或如何解决,javascript,jquery,html,Javascript,Jquery,Html,我正在构建一个选择你自己的冒险游戏(你可能已经看到了我之前关于我遇到的另一个问题的问题)。当用户按下空格键时,我最终将主页隐藏起来,但下一个html元素(播放页面)不会显示。我不确定问题是否出在最初的$(“#play”).show()中或在打印来自数据库对象的提示和选择的代码中 我在r/codinghelp上寻求帮助,但只得到了一些对第一部分有帮助的回复(pressing space最初帮助了nada),但对第二部分没有帮助。(见reddit帖子……你们的反应要快得多)。我一直在反复阅读jque
$(“#play”).show()中代码>或在打印来自数据库对象的提示和选择的代码中
我在r/codinghelp上寻求帮助,但只得到了一些对第一部分有帮助的回复(pressing space最初帮助了nada),但对第二部分没有帮助。(见reddit帖子……你们的反应要快得多)。我一直在反复阅读jquery文档,查看w3schools和其他资源,看看它是否格式不正确(我的代码对我来说似乎100%不错)。我还运行了FireFox developer edition中的调试功能以及文本编辑器附带的调试功能(Visual Studio代码)
首先,我的html:
<div id="main">
<div id="home">
<p> [welcome text and whatever] </p>
</div>
<div id="play">
<div id="prompt"></div>
<div id="options"></div>
</div>
</div>
目前似乎功能齐全。之后是数据库,然后是打印提示和选项的代码:
var currentScene = 0;
window.printCurrentScene = function() {
document.getElementById("prompt").innerHTML = "<div>" + this.database[currentScene].prompt;
options = "";
for (var i = 0, length = this.database[currentScene].options.length; i < length; i++) {
options += "<p> <button onClick='setScene(" + this.database[currentScene].options[i].directTo + ")'>"
+ this.database[currentScene].options[i].content +
"</button> </p>";
}
document.getElementById("options").innerHTML = options + "</div>";
}
window.setScene = function(num) {
currentScene = num;
window.printCurrentScene();
}
window.printCurrentScene();
var currentsecene=0;
window.printCurrentScene=函数(){
document.getElementById(“prompt”).innerHTML=”“+this.database[currentScene]。prompt;
选项=”;
对于(var i=0,length=this.database[currentScene].options.length;i”;
}
document.getElementById(“选项”).innerHTML=options+“”;
}
window.setScene=函数(num){
currentScene=num;
printCurrentScene();
}
printCurrentScene();
现在,如果这一切正常工作,主页将淡出(它会淡出),然后播放页面(场景0)将显示并打印场景(它不会),用户将能够选择一个选项并移动到它指向的场景(“directTo”)
我在vs代码中遇到此错误:
Firefox中也有一个类似的例子,我假设这意味着它无法读取或访问html中id=prompt的div。我不太明白我需要重写什么以便它可以访问“prompt”div,或者这是否是导致问题的原因
抱歉,如果这些内容不可读,我有点累了。非常欢迎编辑。什么时候调用window.printCurrentScene()?它是在加载文档后调用的。正如我们所看到的,错误只是因为DOM中没有加载div元素;在文档加载到DOM中后运行脚本。@Narayanan修复了它!非常感谢你。
var currentScene = 0;
window.printCurrentScene = function() {
document.getElementById("prompt").innerHTML = "<div>" + this.database[currentScene].prompt;
options = "";
for (var i = 0, length = this.database[currentScene].options.length; i < length; i++) {
options += "<p> <button onClick='setScene(" + this.database[currentScene].options[i].directTo + ")'>"
+ this.database[currentScene].options[i].content +
"</button> </p>";
}
document.getElementById("options").innerHTML = options + "</div>";
}
window.setScene = function(num) {
currentScene = num;
window.printCurrentScene();
}
window.printCurrentScene();