2014-02-09 81 views
0

我有這些語法來刷新瀏覽器後顯示隨機文本和圖像。一切正常。請看一下。隨機圖像,刷新後隨機文本顯示

的JavaScript

var quotes=new Array(); 
quotes[0] = "text1"; 
quotes[1] = "Text2"; 
quotes[2] = "text3"; 
quotes[3] = "text4"; 

var q = quotes.length; 
var whichquote=Math.round(Math.random()*(q-1)); 

function showquote(){ 
    document.getElementsByTagName('img')[whichquote].style.display="block"; 
    document.getElementById('quote').innerHTML = quotes[whichquote]; 

} 
showquote(); 

HTML

<div id="quote"></div> 
<div> 
    <img class="image" src="http://www.placehold.it/100x50&text=1" /> 
    <img class="image" src="http://www.placehold.it/100x50&text=2" /> 
    <img class="image" src="http://www.placehold.it/100x50&text=3" /> 
     <img class="image" src="http://www.placehold.it/100x50&text=4" /> 
</div> 

CSS

.image { 
    display: none; 
} 

但後來,我增加了許多更<img scr.../>標籤(未涉及到這個JavaScript的目的)在頁面的不同位置,然後隨機圖像沒有出現。

我嘗試了一種不同的方法,將document.getElementById更改爲document.getElementByClassName,如下面的語法,它不起作用。

var q = quotes.length; 
var whichquote=Math.round(Math.random()*(q-1)); 

function showquote(){ 
    document.getElementByClass('image')[whichquote].style.display="block"; 
    document.getElementById('quote').innerHTML = quotes[whichquote]; 

} 
showquote(); 

然後,我嘗試在每個<img..>標籤添加一個id =「imgShow」

<div id="quote"></div> 
    <div> 
     <img id="imgShow" class="image" src="http://www.placehold.it/100x50&text=1" /> 
     <img id="imgShow" class="image" src="http://www.placehold.it/100x50&text=2" /> 
     <img id="imgShow" class="image" src="http://www.placehold.it/100x50&text=3" /> 
     <img id="imgShow" class="image" src="http://www.placehold.it/100x50&text=4" /> 
    </div> 

而不是document.getElementsByTagName,我如下更改爲document.getElementById,但隨機圖像不顯示,要麼。

任何想法?

var q = quotes.length; 
var whichquote=Math.round(Math.random()*(q-1)); 

function showquote(){ 
    document.getElementById('imgShow')[whichquote].style.display="block"; 
    document.getElementById('quote').innerHTML = quotes[whichquote]; 

} 
showquote(); 

我也嘗試通過添加ID父<div>區分隨機<img src>標籤與頁面內的其他<img src>標籤,然後試圖調用只有<img>該ID裏面,但沒有成功。

HTML

<div id="quote"></div> 
<div id="RefreshImg"> 
    <img class="image" src="http://www.placehold.it/100x50&text=1" /> 
    <img class="image" src="http://www.placehold.it/100x50&text=2" /> 
    <img class="image" src="http://www.placehold.it/100x50&text=3" /> 
    <img class="image" src="http://www.placehold.it/100x50&text=4" /> 
</div> 

的JavaScript

var q = quotes.length; 
var whichquote=Math.round(Math.random()*(q-1)); 

function showquote(){ 
    document.getElementById('RefreshImg img')[whichquote].style.display="block"; 
    document.getElementById('quote').innerHTML = quotes[whichquote]; 

} 
showquote(); 

你有什麼想法?

+1

類的名字似乎是要走的路。請注意,您提供的代碼將'getElementByClass'放置在我認爲您想要的地方'getElementsByClassName' – chiliNUT

+1

順便提一下,每個元素都應該具有唯一的ID。 – ElDoRado1239

+0

如果你像'['text1','text2','text3','text4']創建你的數組,你可以節省空間。 – elclanrs

回答

0

我會去一個稍微不同的方式。

HTML:

<div id='quote'></div> 
<img id='quote-img'> 
<!-- by giving the img tag an id, we can leave other img tags throughout 
    the document untouched when we set our random image --> 

JavsScript:

var quotes = [ 
    {img: "http://www.placehold.it/100x50&text=1", quote: "text1"}, 
    {img: "http://www.placehold.it/100x50&text=2", quote: "text2"}, 
    {img: "http://www.placehold.it/100x50&text=3", quote: "text3"}, 
    {img: "http://www.placehold.it/100x50&text=4", quote: "text4"} 
    /* 
     I've moved the quotes and matching images into objects inside an array 
     That way, we keep stuff neatly together, and it allows for a great deal 
     of flexibility when it comes to adding or removing stuff. 
    */ 
]; 

function showquote() { 
    var q = quotes.length; 
    // var whichquote=Math.round(Math.random()*(q-1));  // Logical error... 
    var whichquote=quote[Math.round(Math.random()*(q-1))]; // ... fixed. 

    /* 
     I've moved the quote selection logic into the showquote function. 
     Again, it allows for more flexibilty: now you could theoretically call 
     showquote multiple times (maybe through setTimeout for example) and 
     end up with a different random quote+image each time. 
    */ 

    document.getElementById('quote').innerHTML = whichquote.quote; 
    document.getElementById('quote-img').src = whichquote.img; 
}; 
showquote(); 
+0

謝謝你,安德魯!我嘗試替換你的代碼,但'quote-img'未定義,'quote'也未定義。這意味着沒有圖像,也沒有文字出現。我想知道 –

+0

呃,只有在文檔被加載後才調用showquote(),對吧?如果HTML解析器還沒有機會看到div和img標籤,那麼document.getElementById()調用將失敗... – Andrejovich

+0

是的,它是正確的,我相信這是一個正確的方法,但缺少一些東西。 –

0

嘗試這樣:

var quotes = []; 
quotes[0] = "text1"; 
quotes[1] = "Text2"; 
quotes[2] = "text3"; 
quotes[3] = "text4"; 

var q = quotes.length; 
var whichquote = Math.floor(Math.random()*q); 

function showquote(){ 
    document.getElementById('img'+whichquote).style.display = ''; 
    document.getElementById('quote').innerHTML = quotes[whichquote]; 
} 
showquote(); 

<div id="quote"></div> 
<div> 
<img id="img0" style="display:none;" src="http://www.placehold.it/100x50&text=1" /> 
<img id="img1" style="display:none;" src="http://www.placehold.it/100x50&text=2" /> 
<img id="img2" style="display:none;" src="http://www.placehold.it/100x50&text=3" /> 
<img id="img3" style="display:none;" src="http://www.placehold.it/100x50&text=4" /> 
</div> 
+0

這不是一個更好的解決方案。每當你添加更多的引號時,你必須添加和id。您可以使用通用類和按索引定位元素來使其更加靈活。 – elclanrs

+0

這不是更好,但我認爲他/她正在尋找「簡單和類似於我的」而不是「最優和乾淨」。 – ElDoRado1239

+0

謝謝你,埃爾多拉多。我嘗試替換你的代碼,什麼也沒有顯示!我想知道爲什麼? –