0

我目前有此代码用于显示随机客户推荐。我想用一个代码替换随机函数,该代码将按顺序显示引号,然后重复它们。

<html style="direction:rtl;">
  <DIV id=textrotator style="FONT: 16px arial ; text-align:right; WIDTH: 100%; COLOR: rgb(255,255,255)"></DIV>
  <body bgcolor="#FFFFFF" alink="#FFFFFF" vlink="#FFFFFF" topmargin="0" leftmargin="0" rightmargin="0">
  <script type = "text/javascript">
    var hexinput = 255; // initial color value.

    quotation = new Array()
    quotation[0] = "text1"
    quotation[1] = "text2"
    quotation[2] = "text3"

    function fadingtext()
    { 
      if(hexinput >111) 
      { 
        hexinput -=11; // increase color value
        document.getElementById("textrotator").style.color="rgb("+hexinput+","+hexinput+","+hexinput+")"; // Set color value.
        setTimeout("fadingtext()",200); // 200ms per step
      }
      else 
      {
        hexinput = 255; //reset hex value
      }
    }

    function changetext()
    {
      if(!document.getElementById){return}
      var which = Math.round(Math.random()*(quotation.length - 1)); 
      document.getElementById("textrotator").innerHTML = quotation[which]; 
      fadingtext();
      setTimeout("changetext()",8000); 
    }

    window.onload = changetext();
  </script>
4

1 回答 1

1

您需要使索引全局化。抛出which函数之外,然后只增加它,确保在结束时换行。

这是“changetext”功能的替代品:

var which = 0;

function changetext()
{
    which += 1; 
    if (which >= quotation.length)
    {
        which = 0;
    }

    document.getElementById("textrotator").innerHTML = quotation[which]; 

    fadingtext();

    setTimeout("changetext()",8000);
}
于 2013-06-12T18:57:26.727 回答