2014-01-07 97 views
1

我想用Javascript格式編寫一個簡單的「選擇你自己的冒險」風格的遊戲,主要是因爲它很簡單,但我遇到了問題。我試圖讓JavaScript運行在頁面加載,但它不會開始。我只剩下一個空白頁面。功能標籤似乎不起作用?

下面是代碼:

<!doctype html> 
<body> 
<title>Dungeons and Dwarves</title> 
<script type="text/javascript> 
    function begin() { 
    alert("Welcome to Dungeons and Dwarves! A free 'Choose Your Own Adventure' game. In this adventure, you will be faced with many decisions, some of which could potentially lead to your death. If you are willing to put your life on the line, and leap forward into a new world and explore the unexplored, press OK to begin.") 
    var myAge = prompt("How old are you, adventurer?") 
    if (myAge < 18) 
    { 
     alert("Oh! They start younger and younger every year! You'll be careful out there, wont you?") 
    } 
    else 
    { 
     alert("I see you have had some experience with adventures! I wish you the best of luck, not that you will need it!") 
    } 
} 
</script> 
<body onload='javascript:begin()'> 
</body> 

林不知道我做錯了這裏,但我認爲功能標籤不能正常工作。就像它在Notepad ++中看起來那樣,這可能就是我。任何人都可以給我協助嗎?

我很抱歉如果我格式化了這個錯誤。

+1

你有2個身體標籤。我假設1st是爲了成爲一個html標籤,也需要在文檔結尾處關閉。 – Archer

+0

@Archer - 你的意思是'head'標籤 – johnnycardy

+0

還有一個缺少的報價。 – Robin

回答

4

瀏覽器認爲你的JavaScript並不是JavaScript的(但其他一些未知樣的腳本):

<script type="text/javascript> 

您從類型屬性省略了第二"

隨着HTML 5中,type屬性可如果你正在使用JavaScript,被遺漏所以只寫:

<script> 

此外,你有兩個<body>開始標記。其中只有一個具有onload屬性。瀏覽器可能會解析第一個,然後忽略第二個。


這些(其他一些錯誤中)將已經由validator回升。

+0

謝謝!我不敢相信我錯過了! – Siinestar

3

你在忘記"

<script type="text/javascript> 

確保你把"在前面的>

<script type="text/javascript"> 

而作爲Quentin notes,你有兩個body標籤。

+0

非常感謝,我對我如何錯過這一點目瞪口呆! – Siinestar

0
type="text/javascript" //add a " at the end 

修正

<script type="text/javascript"> 
+0

謝謝Daguru!不能相信我錯過了! – Siinestar

0

試試這個:

<html> 
<head> 
<title>Dungeons and Dwarves</title> 
<script> 
function begin() 
{ 
    alert("Welcome to Dungeons and Dwarves! A free 'Choose Your Own Adventure' game. In this adventure, you will be faced with many decisions, some of which could potentially lead to your death. If you are willing to put your life on the line, and leap forward into a new world and explore the unexplored, press OK to begin.") 
    var myAge = prompt("How old are you, adventurer?") 
    if (myAge < 18) 
    { 
     alert("Oh! They start younger and younger every year! You'll be careful out there, wont you?") 
    } 
    else 
    { 
    alert("I see you have had some experience with adventures! I wish you the best of luck, not that you will need it!") 
    } 
} 
</script> 
</head> 
<body onload='javascript:begin()'></body> 
</html> 

您在缺少"

<script type="text/javascript> 

你必須在行misplaces的<body>標籤編號2:從那裏刪除身體標記。

您需要將<title>標籤加入<head>標籤。

+1

我想知道爲什麼這是downvoted。 – Archer

+0

@downvoter投降原因將不勝感激。 – Bhushan

+0

謝謝Bhushan!非常感激! – Siinestar

0
<script type="text/javascript> 


<script type="text/javascript">

<body onload='javascript:begin()'></body>
<body onload='javascript:begin();'></body>

所有快訊需要這樣結束