每次刷新页面时,如何使这些引号按顺序显示?
<div id="demo"></div>
var Quotes = new Array();
var authors = new Array();
Quotes[0] = '<h1>"Remarkable"</h1>'
authors[0] = '<h2>- Peter</h2>';
Quotes[1] = '<h1>"Wow"</h1>';
authors[1] = '<h2>- Heather </h2>';
Quotes[2] = '<h1>"Amazing"</h1>';
authors[2] = '<h2>- Kristen</h2>';
Quotes[3] = '<h1>"Great job"</h1>';
authors[3] = '<h2>- Lauren</h2>';
$(document).ready(function(){
var rand = Math.floor(Math.random()*Quotes.length);
$('#demo').html( Quotes[rand]+authors[rand]);
});
答案 0 :(得分:1)
如果我理解正确,每当刷新页面时,您需要按顺序更改引号
var Quotes = new Array();
var authors = new Array();
Quotes[0] = '<h1>"Remarkable"</h1>'
authors[0] = '<h2>- Peter</h2>';
Quotes[1] = '<h1>"Wow"</h1>';
authors[1] = '<h2>- Heather </h2>';
Quotes[2] = '<h1>"Amazing"</h1>';
authors[2] = '<h2>- Kristen</h2>';
Quotes[3] = '<h1>"Great job"</h1>';
authors[3] = '<h2>- Lauren</h2>';
$(document).ready(function() {
var idx = localStorage.getItem('quote-index') != null ? (+localStorage.getItem('quote-index') + 1) : 0;
idx = idx < Quotes.length ? idx : 0;
$('#demo').html(Quotes[idx] + authors[idx]);
localStorage.setItem('quote-index', idx)
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="demo"></div>
&#13;
演示:Fiddle
答案 1 :(得分:0)
为了按顺序执行此操作,我将执行以下操作:
(文档)$。就绪(函数(){ var html =&#39;&#39 ;; // init空值
for(var counter=0;counter<Quotes.length;counter++){
html += "</br>" + Quotes[counter] + authors[counter];
}
//now append it all
$('#demo').html(html);
});
找到了工作小提琴here