2015-10-16 80 views
-2

我是编程新手,我自学HTML和Javascript以及python。 我在学习这些语言方面得到了一点,我觉得我可以使用javascript在html中处理其中一件事情。 我遇到了一个问题,无论出于何种原因,我的代码根本无法工作。 我仔细检查了一切,我很确定它都在正确的位置,所有的字符都是正确的。 这是我的代码到目前为止,完整。我仍处于开发早期阶段,我知道这还不完整。为什么我的脚本不能在这个html文档中工作?

<!doctype html> 
<html> 
<head> 
<title>Python game</title> 
</head> 
<body> 

<p>Enter your choice by clicking on the buttons below the paragraph<p> 

<p id="newText">kjgkhg</p> 
<button type="onClick" id="ChooseFirst">First choice</button> 
<button type="onclick" id="chooseSecond">Second choice</button> 
<button type="onclick" id="choosethird">Third choice</button> 

<script> 
document.getElementById("newText").innerHTML = "why doesn't this work?"; 

funciton darkRoom() { 
    vars x=document.getElementById("newText").innerHTML ; 
    document.getElementById("newText").innerHTML = "You wake up in a dark room with no \ 
    idea how you go there. You can make out the outline of three doors\ 
    labeled '1', '2', and '3' directly in front of you. There is no door behind you.\ 
    Which door do you enter?"; 
} 

function lions() { 
} 

function tiger() { 
} 

functoin bear() { 
} 

function brickRoad() { 
} 

function quickSand() { 
} 

function sizePuzzle() { 
} 

function riddlesOnWall() { 
} 

function wolfSheepCabbage() { 
} 

function duckHunt() { 
} 

function hangman() { 
} 

function goldRoom() { 
} 

function ocean { 
} 

function winScreen() { 
} 

function youDie() { 
} 

</script> 
</body> 

</html> 
+0

发挥你有很多简单的错别字在你的代码。另外,javascript区分大小写 –

+3

打开浏览器的JavaScript控制台。错误在那里打印。 – JJJ

+0

虽然我在这里正确使用骆驼案例。在区分大小写的情况下我搞砸了什么? – Steve

回答

3

湖中有很多错别字克里斯大号已经指出并不亚于Juhana是正确的,但在打印到控制台上的错误是难以破译的初学者(尽管你学习他们特别是作为初学者!)。

下面是一些精简模板,你可以用

<!doctype html> 
<html> 
<head> 
<title>Python game</title> 
<script> 
function darkRoom() { 
    var x=document.getElementById("newText").innerHTML ; 
    // You cannot escape the end-of-lines you have to concatenate individual strings 
    document.getElementById("newText").innerHTML = "You wake up in a dark room with no " + 
    "idea how you go there. You can make out the outline of three doors" + 
    "labeled '1', '2', and '3' directly in front of you. There is no door behind you." + 
    "Which door do you enter?"; 
} 
// instead of alert() call another function reacting to the users input 
function firstChoosen() {alert("first choosen");} 
function secondChoosen() {alert("second choosen");} 
function thirdChoosen() {alert("third choosen");} 
</script> 
</head> 
<body onload="darkRoom()"> 
<p>Enter your choice by clicking on the buttons below the paragraph<p> 
<p id="newText"> </p> 
<!-- there are better methods but it's ok for now --> 
<button onclick="firstChoosen()" id="ChooseFirst">First choice</button> 
<button onclick="secondChoosen()" id="chooseSecond">Second choice</button> 
<button onclick="thirdChoosen()" id="choosethird">Third choice</button> 
</body> 
</html> 
+0

@Steve你可以使用你想要的东西,它是一个模板和'alert()'占位符,无论你的思想能够想出什么来通知用户结果;从一个很好的弹出窗口到铃声,一切皆有可能。 – deamentiaemundi

+0

很酷。我将尝试将它包含在每个单独的函数中。将它包含在自己的功能中会更好吗?值得把这个脚本放在它自己的文件中吗? – Steve

+0

对于每个按钮都不需要单独的函数,您可以在一个函数中完成所有操作,并使用该函数的参数来告诉哪个按钮已被选为某个问题的答案。让我们说'answerToQuestion(buttonNumber,QuestionNumber){...}'。你可以使用一个长的switch()来实现它,或者使用一个数据库(例如:如果你想留在JavaScript中,使用JSON),你可以在其中存储所有的问题和答案。从另一种语言,如果你不明白*两种*语言。如果你陷入困境:问。 – deamentiaemundi

相关问题