2013-05-08 68 views
0

我正在寫一個函數,當被調用時打印出相同的圖像7次。當調用函數時,我在參數中傳遞一個0。下面是函數:Javascript獲取多個圖像而不是一個

function showCards(numcards) { 
    while (numcards < 7) { 
    var data = "" 
    data += "<td><img src='http://www.biogow.com/images/cards/gbCard52.gif' NAME='card0' ></td>" 
    numcards = numcards + 1 
    } 
    return (data) 
} 

然而,當這個函數被調用,只打印一張圖像。我如何才能打印所有七個?有任何想法嗎?謝謝!

回答

6

var data = ""放在循環外部。原樣,每次迭代都會重置。

function showCards(numcards) 
{ 
    var data = "" 
    while (numcards < 7) 
    { 
     data += "<td><img src='http://www.biogow.com/images/cards/gbCard52.gif' NAME='card0' ></td>" 
     numcards = numcards + 1 
    } 
    return (data) 
} 
相關問題