关于如何将作者(在下一行)添加到引号中,我有点担心。
我有一个数组并添加了评论以询问此问题。
<script type = "text/JavaScript">
var quote = new Array();
quote[0] = ' quote 1 Lorem ipsum blah blah ' ;
quote[1] = ' quote 2 Nullam commodo blah blah ' ;
quote[2] = ' quote 3 Sed vitae blah blah ' ;
quote[3] = ' quote 4 Maecenas blah blah ' ;
quote[4] = ' quote 5 Fusce lorem velit, blah blah ' ;
quote[5] = ' quote 6 Ut blah blah ' ;
quote[6] = ' quote 7 Phasellus blah blah ' ;
// authors: Name[x] = ' name '; ?
var quotelen = quote.length;
// Pick a random index number
// Is this how to pick a random number from the array?
// var quotelen = Math.floor(Math.random() * quote.length);
// The Start Date (yyyy, m, d) m=0=January, m=1=February
var firstDate = new Date(2016,0,1);
// Today
var today = new Date();
// Difference in days
var diff = Math.floor((today - firstDate)/1000/60/60/24);
// Calculate the index of the quote of the day
while(diff >= quotelen){
// Restart the array index if the difference is greater than the array's length
diff = diff - quotelen;
}
// The quote of the day
var todayQuote = quote[diff];
onload = function(){document.getElementById('quote').firstChild.data = todayQuote}
// how to add Authors to the Quotes? ... getElementById(' Name ').firstChild.data = todayName ?
</script>
</head>
<body>
<div id = 'quote'> </div>
对此的任何帮助都会很棒。 欢呼声。
答案 0 :(得分:0)
您可以推送一个对象,而不是将quotes
作为字符串推送。而不是循环来获得差异,只需使用%
运算符。
{ quote: 'quote 1 Lorem ipsum blah blah' , aurthor: 'foo1' },
function getTodaysQuote(quote) {
var firstDate = new Date(2016, 0, 1);
var today = new Date();
// Difference in days
var index = Math.floor((+today - +firstDate) / (1000 * 60 * 60 * 24)) % quote.length;
return quote[index]
}
function printQuote(quote) {
document.querySelector('.quote').innerHTML = quote.quote;
document.querySelector('.author').innerHTML = "-" + quote.author;
}
function main() {
var quotes = [
{ quote: 'quote 1 Lorem ipsum blah blah' , author: 'foo1' },
{ quote: 'quote 2 Nullam commodo blah blah' , author: 'foo2' },
{ quote: 'quote 3 Sed vitae blah blah' , author: 'foo3' },
{ quote: 'quote 4 Maecenas blah blah' , author: 'foo4' },
{ quote: 'quote 5 Fusce lorem velit, blah blah' , author: 'foo5' },
{ quote: 'quote 6 Ut blah blah' , author: 'foo6' },
{ quote: 'quote 7 Phasellus blah blah', author: 'foo7' },
]
var q = getTodaysQuote(quotes);
printQuote(q);
}
main();
&#13;
.quote {
font-size: 20px;
font-weight: bold;
color: #333;
margin: 0;
}
.author {
float: right;
margin:0;
}
.content{
width: 50%;
}
&#13;
<div class="content">
Todays quote is:
<p class="quote"></p>
<p class="author"></p>
</div>
&#13;