2015-10-16 76 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

相關問題