2012-02-20 105 views
0

我一直在擺弄這個HTML和JavaScript一兩個小時......現在我弄不明白爲什麼它不起作用。我一直在嘗試自己學習html,css和javascript ......但我不認爲Eclipse正在調試我的東西......發生了什麼事情?用這個HTML/Javascript/CSS生成一個隨機字符串?

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> 
<style type="text/css"> 
a:link {color:#FF0000;} /* unvisited link */ 
a:visited {color:#00FF00;} /* visited link */ 
a:hover {color:#FF00FF;} /* mouse over link */ 
a:active {color:#0000FF;} /* selected link */ 
</style> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Catlard.com</title> 

<link rel=StyleSheet href="styles/menuStyle.css" type="text/css"/> 

<script type="text/javascript"> 

function RandomQuote() 
{ 
    var quotes= new Array(); 
    quotes[0] = "...believes it may be useful in a time of need." 
    quotes[1] = "...knows you have a problem, but accepts you anyway." 
    quotes[2] = "...believes the aliens were involved at Oak Island." 
    quotes[3] = "...demands to know the location of your hidden rebel base!" 
    quotes[4] = "...SAW you take the cookie from the cookie jar." 
    return quotes[Math.floor(Math.random() * 4.99); 
} 

</script> 

</head> 
<body> 

<div id="framecontent"> 
<div class="innertube"> 
<h1>CSS Top Frame Layout</h1> 
<span style="font-family : Courier;color: #000000;"> 
<a href="resume.html"> Resume </a> 
<a href="http://catlard.blogspot.com"> Blog </a> 
<a href="pixelating.html"> Arts n' Farts</a> 
<a href="contact.html"> Contact </a> 
<a href="typing.html"> Games </a> 
</span> 
</div> 
</div> 


<div id="maincontent"> 
<div class="innertube"> 

document.write(RandomQuote()); 
<p style="text-align: center">Blah blah blah </p> 
</div> 
</div> 

</body> 
</html> 
+0

你沒有告訴我們當你運行這個時會發生什麼。問題是什麼? – Beska 2012-02-20 22:33:50

+0

啊,對不起。當我運行它時,它在瀏覽器中沒有顯示任何我要調用的函數。 – Catlard 2012-02-20 22:35:05

+0

因爲你不*調用*你的代碼。它被格式化爲文本。嘗試將其放入另一個腳本標記中。 -1缺乏r? – 2012-02-20 22:35:13

回答

1

您在返回報價單行上有錯誤。它應該關閉)];不);

另外,每行引號[n]行應以分號結尾。

而且,您應該在script標記中包含document.write()。

+0

是的,只是失蹤]結束 – Dampsquid 2012-02-20 22:36:29

+0

@Dampsquid編輯澄清的意思。謝謝。 – Jordan 2012-02-20 22:39:59

+0

啊,這是完全正確的。我做了所有這些事情,現在它正在工作。我爲我所有的語法錯誤表示歉意 - 我沒有一個非常好的IDE - 我現在正在使用Eclipse for Javascript。任何關於OSX 32位初學者的建議? – Catlard 2012-02-20 22:42:04

1

您的「document.write」行需要位於<script>區域。你也錯過了你的迴歸線上的「]」括號。

+0

已經完成了,謝謝。 – Catlard 2012-02-20 22:43:18

0

你需要更清楚地檢查你的代碼。也許使用一種語法高亮的編程編輯器,如SciTe。

即使你沒有明確地告訴我們你(剛纔說的它不工作),在跳出我的第一件事就是這一行的問題:

return quotes[Math.floor(Math.random() * 4.99); 

你忘了結尾]上陣列。

+0

啊,是的,確切的。非常感謝你。我會檢查SciTe,而不是日食。 Cheerio! – Catlard 2012-02-20 22:42:49

0

你錯過了結局]在返回命令。

+0

謝謝!除此之外,這是一個大問題。 – Catlard 2012-02-20 22:43:55

0

兩件事:

return quotes[Math.floor(Math.random() * 4.99)]; 

您缺少您的報價陣列的結束標記。

<script language="javascript" type="text/javascript"> 
    document.write(RandomQuote()); 
</script> 

您的JavaScript必須包裝在該腳本標記中,否則它只是呈現爲HTML。