显示随机项目从数组上按一下按钮,没有两个在一排
Display Random Item From Array On Button Click, NO TWO IN A ROW
我有一个按钮,显示来自数组的引用和作者。我需要按钮来显示随机引用/作者每次按钮被单击。没有两个相同的引用/作者连续出现!
window.onload = function()
{
//assign var to quoteText id contents
var quoteSpan = document.getElementById("quoteText");
//assign var to authorText id contents
var authorSpan = document.getElementById("authorText");
//assign var to submitButton contents
var submitButton = document.getElementById('submit');
var quotes = [
{'text': '"Whatever you are, be a good one."', 'author': '-Abraham Lincoln'},
{'text': '"It has been my philosophy of life that difficulties vanish when faced boldly."', 'author': '-Isaac Asimov'},
{'text': '"Enjoy life. There’s plenty of time to be dead."', 'author': '-Anonymous'},
{'text': '"Every moment is a fresh beginning."', 'author': '-T.S. Eliot'},
{'text': '"One day your life will flash before your eyes. Make sure it is worth watching."', 'author': '-Anonymous'}
];
var oldQuoteIndex = -1; //unfound item in array is -1
//function determining random quote
function nextQuote() {
do {
var newQuoteIndex = Math.floor(Math.random() * quotes.length); //picks random quote index from quotes array
} while (newQuoteIndex == oldQuoteIndex); //while index of newly chosen quote is the same as the index of old quote
quoteSpan.innerHTML = quotes[newQuoteIndex].text; //make HTML's quoteText random quote
authorSpan.innerHTML = quotes[newQuoteIndex].author; //make HTML's authorText random author
var oldQuoteIndex = newQuoteIndex; //make old index same as new index, so that next time it runs, the WHILE aspect causes DO aspect to randomize
}
//when button is clicked, quotation function starts
submitButton.onclick = nextQuote;
}
你在几个地方重新声明变量,而不是在更高的作用域中使用变量,这就是为什么你不是每次都得到一个新的引用。
window.onload = function() {
var quoteSpan = document.getElementById("quoteText");
var authorSpan = document.getElementById("authorText");
var submitButton = document.getElementById('submit');
var oldQuoteIndex = -1;
var newQuoteIndex = -1;
var quotes = [
{'text': '"Whatever you are, be a good one."', 'author': '-Abraham Lincoln'},
{'text': '"It has been my philosophy of life that difficulties vanish when faced boldly."', 'author': '-Isaac Asimov'},
{'text': '"Enjoy life. There’s plenty of time to be dead."', 'author': '-Anonymous'},
{'text': '"Every moment is a fresh beginning."', 'author': '-T.S. Eliot'},
{'text': '"One day your life will flash before your eyes. Make sure it is worth watching."', 'author': '-Anonymous'}
];
function nextQuote() {
while (newQuoteIndex == oldQuoteIndex) {
newQuoteIndex = Math.floor(Math.random() * quotes.length);
}
quoteSpan.innerHTML = quotes[newQuoteIndex].text; //make HTML's quoteText random quote
authorSpan.innerHTML = quotes[newQuoteIndex].author; //make HTML's authorText random author
oldQuoteIndex = newQuoteIndex;
}
submitButton.onclick = nextQuote;
}
小提琴
/* getNewQuote might be good in a js file to use on any page */
function getNewQuote(callback){
var newquote;
var quotes = getNewQuote.prototype.quotes;
do{
newquote = quotes[parseInt(Math.random()*quotes.length)];
/* if quotes.length == 1 we'll loop forever */
}while( quotes.length > 1 && !getNewQuote.prototype.isLastQuote(newquote));
/* Good idea to check if there is a callback */
if(callback){
callback(newquote);
}
}
getNewQuote.prototype.quotes = [
{'text': '"Whatever you are, be a good one."', 'author': '-Abraham Lincoln'},
{'text': '"It has been my philosophy of life that difficulties vanish when faced boldly."', 'author': '-Isaac Asimov'},
{'text': '"Enjoy life. There’s plenty of time to be dead."', 'author': '-Anonymous'},
{'text': '"Every moment is a fresh beginning."', 'author': '-T.S. Eliot'},
{'text': '"One day your life will flash before your eyes. Make sure it is worth watching."', 'author': '-Anonymous'}
];
getNewQuote.prototype.lastQuote = null;
getNewQuote.prototype.isLastQuote = function(quote){
if(!getNewQuote.prototype.lastQuote
|| ( getNewQuote.prototype.lastQuote.author != quote.author
&& getNewQuote.prototype.lastQuote.text != quote.text )
) {
return getNewQuote.prototype.lastQuote = quote;
}
return null;
}
/* End of the getNewQuote */
/* This goes on the page */
submitButton.onclick = function (){
/* pass getNewQuote a callback function to update the elements with the new quote */
getNewQuote(function(quote){
quoteSpan.innerHTML = quote.text;
authorSpan.innerHTML = quote.author;
});
}
/* if you decide to go with jQuery you can replace the above onclick with this */
$(function(){
$("#submitButton").click(function (){
getNewQuote(function(quote){
$("#quote").html(quote.text);
$("#author").html(quote.author);
});
});
});
相关文章:
- jQuery对象从html表中查询为两个一维数组,用于Chartist图表
- 有没有一种方法可以从两个标签之间提取文本,并以我选择的格式输出
- 在一次点击中发布到两个表单 JavaScript
- 如何调用两个函数是一种html输入类型
- 效率:整数数组的一个子集中两个项之间的最大差值
- 将具有两个此类子字符串的字符串中的第一个子字符串生成与正则表达式匹配,正则表达式通常默认匹配最后一个子字符串
- 蛋糕PHP,一键发布到两个网址
- 有没有一种方法可以找到一个匹配两个不同填充的文档,并在findOne()中获取他的文档
- 如何将两个事件的变量组合为一,作为两者的总和.jquery
- 一种在javascript上获得具有两个输入(字符串)的散列键的方法
- 合并两个Javascript数组,一次合并三个元素
- 有没有一个lodash函数可以合并两个对象并删除其中一个对象的属性(如果它们没有)'不存在于另一个中
- 同时换掉两个Div,换成另一个Div并保持循环一段固定的秒数
- 我可以用JavaScript声明一个数组中的两个变量吗
- Kendo Grid UI水印列或一列的两个下拉列表
- AngularJS:用两个JSON数组填充复选框列表,最初检查了一个数组中的一些项
- 如何一次插入两个表?准备好的发言
- 使用getElementById一次指定两个操作
- Javascript使用两个键排序数组
- 显示随机项目从数组上按一下按钮,没有两个在一排