2013-05-17 20 views
0

首先,這可能會有點長。 我正在嘗試使用javascript製作RPG文字遊戲。來自javascript的換行符/樣式文本?

在我的HTML中,我擁有的是html,head,css,body,div和script標記。

<!DOCTYPE html> 
<html> 
<head> 
<link rel="stylesheet" type="text/css" href="story_css.css"> 
</head> 
<body> 
<div class="container"> 
<script src="story_js.js"></script> 
</div> 
</body> 
</html> 

因此,這將純粹是我的遊戲信息的容器。

然後,我有我的javascript:

var ATTACK = 0; 
var STRENGTH = 0; 
var DEFENSE = 0; 
var RANGED = 0; 
var MAGIC = 0; 
var AGILITY= 0; 
var HEALTH = 0; 
var CLASS = prompt("Choose a class (class only effects starting stats, but all classes can learn the same abilities at the same rate):Warrior, Mage, Theif, Archer.").toUpperCase(); 
switch (CLASS){ 
    case "WARRIOR": 
     ATTACK = 16; 
     STRENGTH = 18; 
     DEFENSE = 17; 
     RANGED = 2; 
     MAGIC = 2; 
     AGILITY = 5; 
     HEALTH = 200; 
     break; 

    case "ARCHER": 
     ATTACK = 6; 
     STRENGTH = 5; 
     DEFENSE = 11; 
     RANGED = 20; 
     MAGIC = 3; 
     AGILITY = 15; 
     HEALTH = 175; 
    break; 
} 

document.write("These are the levels for each of your characters skills. You may write these down and keep track of them, but the game will automatically record your progress for you. However, you will not be able to see your levels until you type a command that asks for them, or if you require a certain level to complete a task.") 
var NAME = prompt("Enter your name:"); 

confirm("You are about to embark on an epic quest... so, " + NAME + " are you ready to begin?"); 

document.write("It is the year 3355, according to the Delil calendar, and you awake from your night's sleep. Today is a special day. It is your 18th birthday! In your small village of Shadowhollow, you are one of the 23 people who live there. But as of today, you are one of the villages men."); 

我的問題是,當JavaScript是做文件撰寫的部分,它只是把它作爲一個巨大的一塊文本,如果前夕,他們從不同的來了提示。有人能幫助我,告訴我如何設計它嗎?

+3

你應該切換到的使用['.innerHTML'](https://developer.mozilla.org/en-US/docs/DOM/element.innerHTML)代替'文件.WRITE()'。 – Sirko

+0

[createElement](https://developer.mozilla.org/en-US/docs/Web/API/document.createElement?redirectlocale=en-US&redirectslug=DOM%2Fdocument.createElement)同樣也是建議的 – sabithpocker

+0

停止使用'document .WRITE()'!你不會對它感到滿意,因爲它不會做你認爲它的做法。最重要的是它會覆蓋整個頁面的內容,刪除所有內容,包括正在運行的腳本。 – RoToRa

回答

0
document.write("<h2>A sample heading</h2>\ 
<p>These are the levels for each of your characters skills.\ 
You may write these down and keep track of them, but the game will automatically record your progress for you.\ 
However, you will not be able to see your levels until you type a command that asks for them, \ 
or if you require a certain level to complete a task.</p>"); 
var NAME = prompt("Enter your name:"); 

confirm("You are about to embark on an epic quest... so, " + NAME + " are you ready to begin?"); 

document.write("<h2>A sample heading</h2>\ 
<p>It is the year 3355, according to the Delil calendar, and you awake from your night's sleep. \ 
Today is a special day. It is your 18th birthday! In your small village of Shadowhollow, you are one of the 23 people who live there. \ 
But as of today, you are one of the villages men.</p>"); 

您可以使用backslash分裂了在JavaScript中不同行的字符串。

您可以使用<p><h1>的不同文字標記標記作爲標題1等文字的樣式,而文字又是HTML的一部分。爲了更加穩重,你可以看看css

如果您想在javascript中使用換行符,請使用\n\t作爲製表符。這僅用於輸出格式。

如果你想在HTML結果換行符,使用<br>

另外,document.write方法已被棄用,不建議,如果 你是認真對待您的編程。你必須求助於更好的 DOM操作方法,如document.createElementappendChild 等,以動態創建文檔。

Dynamically create a HTML form with Javascript

0

爲什麼你不HTML結構文本?:

document.write('<p>...</p>'); 
... 
document.write('<p>...</p>');