Javascript会显示多个图像而不是一个

时间:2013-05-08 04:32:37

标签: javascript

我正在编写一个函数,在调用时会打印7次相同的图像。调用函数时,我在参数中传递0。这是功能:

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)
}

但是,调用此函数时,它只打印一个图像。我如何才能打印所有七个?有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分: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)
}