2013-02-13 27 views
-1

以及即時通訊新的JavaScript我已經完成了C++ C#VB。但是這個javascript書很難理解,它只是練習沒有完整的代碼來看待。無論如何,我正在做一個簡單的項目,但我不明白我要去哪裏錯了。 CDATA是我認爲我錯了的地方。這是我的第二個項目,第一個很容易,直到CDATA和Var得到Stuck。我知道這段代碼已經過時了,但是我正在學習這本書來學習基本的JS,並且繼續努力。繼承人的代碼:我有問題與CDATA在javascript中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Largest Islands</title> 
<meta http-equiv="content-type" content="text/html; charset=iso- 
8859-1" /> 
</head> 
<body> 
<h1>Largest Islands</h1> 
<script type="text/javascript"> 
/* <![CDATA[ */ 
var island1Name = "Greenland"; 
var island2Name = "New Guinea"; 
var island3Name = "Borneo"; 
var island4Name = "Madagascar"; 
var island5Name = "Baffin"; 
var island1Size = 2175600; 
var island2Size = 790000; 
var island3Size = 737000; 
var island4Size = 587000; 
var island5Size = 507000; 
document.write("<p>The largest island 
in the world is " + island1Name 
+ " with " + island1Size + " miles.</p>"); 
document.write("<p>The second largest island 
in the world is " + island2Name 
+ " with " + island2Size + " miles.</p>"); 
document.write("<p>The third largest island 
in the world is " + island3Name 
+ " with " + island3Size + " miles.</p>"); 
document.write("<p>The fourth largest island 
in the world is " + island4Name 
+ " with " + island4Size + " miles.</p>"); 
document.write("<p>The fifth largest island 
in the world is " + island5Name 
+ " with " + island5Size + " miles.</p>"); 
/* ]]> */ 
</script> 
</body> 
</html> 

它是一個非常簡單的項目我只需要一些幫助,找出我出錯的地方。預先感謝您

+1

不,CDATA不是你要出錯的地方。它應該做什麼?它不正確的做法是什麼?你有錯誤嗎? – ceejayoz 2013-02-13 17:38:52

+0

那麼有什麼不行? – Bergi 2013-02-13 17:39:33

回答

0

我認爲你的問題是這裏的換行符。 您的代碼:

document.write("<p>The largest island 
in the world is " + island1Name 
+ " with " + island1Size + " miles.</p>"); 

應該是:

document.write("<p>The largest island in the world is " + island1Name 
+ " with " + island1Size + " miles.</p>"); 

如果你開始以線串和你不一樣最終它會導致線路斷裂投擲的錯誤。 除此之外,我建議你使用firefox進行開發,並與螢火蟲一起調試你的腳本。

0

在本地運行它,JavaScript錯誤控制檯使問題非常清楚。它甚至給你線數。

document.write("<p>The largest island 
in the world is " + island1Name... 

您不能在JavaScript字符串中間換行。

document.write("<p>The largest island in the world is " + island1Name... 

工程。您需要修復所有這些問題,並在每個主要瀏覽器中開始使用可用的開發工具。

0

我認爲這個問題是來自破碎串線

而不是

document.write("<p>The largest island 
in the world is " + island1Name 

嘗試

document.write("<p>The largest island" 
+ "in the world is " + island1Name 
0

你的問題是,像這樣的多行字符串不允許在JavaScript:

document.write("<p>The second largest island 
in the world is " + island2Name 

如果您在錯誤的地方擺脫了這些新行,代碼將起作用。該行現在看起來是這樣的:

document.write("<p>The second largest island in the world is " + island2Name 

例子:http://jsbin.com/igowel/1/edit

要繼續使用JavaScript,習慣使用控制檯和開發工具。對於Chrome,看看這個Q & A:How do I use the JavaScript Console in Google Chrome?

+0

謝謝。這很奇怪,因爲我的書告訴我要在那裏放假 – user2069296 2013-02-13 18:52:07

+0

說實話,它看起來像一本真正的舊書。雖然開始使用vanilla JavaScript是非常好的,但在開始使用jQuery時非常方便。如果這本書大約有2-3年的歷史,那麼您可能希望獲得一本更新的書籍,比如jQuery in Action(實際上,無論如何我都會這麼建議)。 – Cymen 2013-02-13 20:24:49