2011-04-19 14 views
0

我有一個帶有一個縮略圖塊(小圖像)的頁面,我希望在單擊一個小圖像時出現圖像的較大版本。兩個文件具有相同的名稱,但位於不同的文件夾中問題是,縮略圖是使用for循環隨機生成的。這裏是我工作的腳本(的onclick):JS:訪問隨機分配的img屬性

function seeFoto(that){ 
document.getElementById("photoViewer").style.zIndex="1"; 
document.getElementById("gallery").style.zIndex="0"; 
document.getElementById("bigpic").src=.../// <<-- problem: how do I get the source of the thumbnail and then replace "bigpic" with other version of the image?? 

這裏是給每個小區的隨機圖像腳本(它的工作原理):

function randomThumbs(){ 
swapp(); 
var onecell="1"; 
for (onecell=1; onecell<33; onecell++){ 
var duh=Math.ceil(Math.random()*802); 
document.getElementById("img" +onecell).setAttribute("src","thumbs/"+duh+".jpg"); 
}} 

swapp()是一個函數,隨機選擇兩個單元格並將圖片更改爲另一個隨機圖片。 HTML代碼:

<td><a id="link1" href="page.html" <img id="img1" src="thumbs/1.jpg" onclick="seeFoto(this)" /></a></td> 
<td><a id="link2" href="page.html" <img id="img2" src="thumbs/2.jpg" onclick="seeFoto(this)" /></a></td> 

希望我的問題是不夠清楚...... 謝謝!

+0

'VAR onecell = 「1」;'爲什麼一個字符串..爲什麼呢?除此之外;不需要兩次初始化。儘管這個地方的'var i'在函數的開頭就完全等於'var i',常用的方法是for(var i = 0; i ThiefMaster 2011-04-19 22:35:45

+0

@ThiefMaster它沒有工作,沒有引號1. – Ishikawa 2011-04-19 22:44:06

+0

我沒有讀你整個代碼,所以不知道是否有更多的問題(有 - 我的改變建議只是爲了擺脫無用的代碼) - 但我剛剛注意到了這一點,並且想要告訴你,因爲編寫好的/乾淨的代碼總是一件好事。 – ThiefMaster 2011-04-19 22:47:30

回答

0
document.getElementById("bigpic").src = that.src.replace('thumbs/', 'bigimages/'); 

我想... :-)

+0

好主意!但它會拋出錯誤:「that」未定義 – Ishikawa 2011-04-19 22:48:47

+0

順便說一句。將「>」添加到您的a元素 – kempsam 2011-04-19 23:01:24

+0

現在可以工作;非常感謝 :) – Ishikawa 2011-04-20 00:40:39