2012-10-01 16 views
-1

我想生成一些報價文本和圖片是隨機的。有人可以驗證此代碼,因爲它不起作用。當按下提交沒有任何反應。我想要做的是輸出一個報價生成器與更改文字

<html> 
    <head> 
    <title>quote generator Javascript</title> 
    </head> 
    <body> 

    <script type="text/javascript"> 

    quote = new Array() 

    quote[0] = 'img/einstein.jpg'; 
    quote[1] = 'img/einstein.jpg'; 
    quote[2] = 'img/einstein.jpg'; 
    quote[3] = 'img/einstein.jpg'; 

    text = new Array() 

    text[0] = 'hallo1'; 
    text[1] = 'hallo2'; 
    text[2] = 'hallo3'; 
    text[3] = 'hallo4'; 

    function popup(){ 
     if(['submit']){ 

      document.write('<a href="' + text[Math.floor(Math.random() * text.lenght)]; + '" ><img src="' + quote[Math.floor(Math.random() * quote.lenght)]; '" style="border:0px;" >'); 
     } 
    } 


    </script> 

    <input type="button" value="random quote" id="submit" onclick="popup()"> 

    </body> 
    </html> 

回答

1

由於您的if條件,您的代碼不起作用。
由於

(['submit'] == true) // false 

你永遠不執行document.write語句,以便只是刪除if。此外

quote.lenght 

應改爲

quote.length 

同上text

最後,而不是文件撰寫,使用innerHTML方法或createElement/appendChild方法正確注入您的標記

+0

你的意思是這樣這個? 功能彈出(){ \t \t \t的document.getElementById( '提交')的innerHTML = ''; \t \t \t} \t \t} – user1703601

+0

你不能使用innerHTML方法對於像'input'自動關閉元素 – fcalderan

0

Fabrizio的回答指出你的鱈魚的大部分問題即我認爲你不需要

if(['submit']) 

行,因爲彈出功能永遠不會執行,除非按下提交按鈕。

此外,產生的隨機數的時候,你需要做的是:

text[Math.floor(Math.random() * (text.length-1))] 

這是因爲text.length = 4,但沒有文字[4]

相關問題