当天指定作者的 JavaScript 名言

标签 javascript random quote

它看起来非常好,因为它保持锁定到特定日期并且刷新时不会改变,但我想将作者添加到数组中。 如:

报价 //作者

作者显示在下一行,以及随机引用顺序。

有什么建议吗?

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

最佳答案

我知道这是迟到的答案。

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

关于当天指定作者的 JavaScript 名言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539338/

相关文章:

windows - 在 Windows 上为 GNU Make 定义和使用构建时间戳

Javascript 范围(或其他)问题

javascript - WebGL 重置顶点位置

sql - postgresql:在标题上强制引号

以随机位置开始的 Javascript 对象

python - 打印生成的随机字符串

sql - 带单引号的 ADF 数据流连接表达式

javascript - 模板文字 HTML TABLE 中的三元运算符

javascript - 将 ES6 类与 ES5 代码一起使用

javascript - 通过id获取元素不起作用