javascript
  • random
  • quote
  • 2016-11-11 26 views 1 likes 
    1

    它看起來非常好,因爲它保持鎖定在特定的一天,刷新時不會改變,但我想將作者添加到數組中。 如:JavaScript當天分配作者的行情

    報價 //作者

    與作者顯示在下一行,以及隨機的報價順序。

    任何提示?

    <script type="text/JavaScript"> 
    var quote = new Array(); 
    quote[0] = 'quote 1 Lorem ipsum dolor sit amet, consectetuer adipiscing  elit.'; 
    quote[1] = 'quote 2 Nullam commodo venenatis elit. In aliquam.'; 
    quote[2] = 'quote 3 Sed vitae risus ac urna pharetra tristique.'; 
    quote[3] = 'quote 4 Maecenas id mi quis nisl porta sodales.'; 
    quote[4] = 'quote 5 Fusce lorem velit, tempor sit amet, luctus nec, suscipit at, velit'; 
    quote[5] = 'quote 6 Ut pellentesque mauris non justo. In varius.'; 
    quote[6] = 'quote 7 Phasellus non urna dignissim nisl mollis consectetuer.'; 
    var qlen = quote.length; 
    var firstDate = new Date(2005,0,1);//start date (yyyy,m,d) - m=0=January, m=1=February 
    var today = new Date();//today 
    var dif = Math.floor((today-firstDate)/1000/60/60/24);//difference in days 
    while(dif>=qlen){//calculate the index of the quote of the day 
    dif=dif-qlen;//restart the array index if the difference is greater that the array's length 
    } 
    var todayQuote = quote[dif];//the quote of the day 
    onload = function(){document.getElementById('q').firstChild.data = todayQuote} 
    </script> 
    </head> 
    <body> 
    <div id='q'>&nbsp;</div> 
    </body> 
    </html> 
    
    +0

    您可以使用json。 quote [0] = {quote:'some text',author:'name'} ;. – Hosar

    回答

    0

    我知道這是遲到的答案。

    <script type="text/JavaScript"> 
    
         var quote = new Array(); 
         quote[0] = ['quote 1 Lorem ipsum dolor sit amet, consectetuer adipiscing  elit.',"some text"]; 
         quote[1] = ['quote 2 Lorem ipsum dolor sit amet',"some text"]; 
    
         var qlen = quote.length; 
         var firstDate = new Date(2005,0,1);//start date (yyyy,m,d) - m=0=January, m=1=February 
         var today = new Date();//today 
         var dif = Math.floor((today-firstDate)/1000/60/60/24);//difference in days 
         while(dif>=qlen){//calculate the index of the quote of the day 
          dif=dif-qlen;//restart the array index if the difference is greater that the array's length 
         } 
    
         var todayQuote = quote[dif]; 
    
         onload = function(){document.getElementById('q').firstChild.data = todayQuote} 
        </script> 
    
    相關問題