我正在使用免费代码营高空广告"建立随机报价机"。我已经尝试过搜索和查看不同的教程,但我似乎无法显示我的随机引用。我想我已经接近但经过几个小时的努力,我想我会问专家!我知道onClick正在工作,因为如果我将newQuote放在引号中它会显示我想要它的位置,但我似乎没有正确调用变量。
$(document).ready(function() {
generator();
function generator() {
var quotes = ["Never make permanent decisions", "Knowledge is having the right answer. Intelligence is asking the right question", "I am Strong, Because i’ve been weak", "Scientists have discovered a food that diminishes a woman’s sex drive by 90%…", "I may look calm, but in my head I’ve killed you 3 times"];
var newQuote = [Math.floor(Math.random() * quotes.length)]
}
$(".btn").on("click", function() {
$('#output').html(newQuote);
});
});
答案 0 :(得分:4)
$(document).ready(function() {
function generator() {
var quotes = ["Never make permanent decisions", "Knowledge is having the right answer. Intelligence is asking the right question", "I am Strong, Because i’ve been weak", "Scientists have discovered a food that diminishes a woman’s sex drive by 90%…", "I may look calm, but in my head I’ve killed you 3 times"];
return quotes[Math.floor(Math.random() * quotes.length)];
}
$(".btn").on("click", function() {
$('#output').html(generator());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn">btn</button>
<span id="output"></span>
答案 1 :(得分:1)
您需要初始化然后设置值。
$(document).ready(function() {
// Initialize then set variables
var quotes;
var newQuote;
generator();
// Sets the variables
function generator() {
quotes = ["Never make permanent decisions", "Knowledge is having the right answer. Intelligence is asking the right question", "I am Strong, Because i’ve been weak", "Scientists have discovered a food that diminishes a woman’s sex drive by 90%…", "I may look calm, but in my head I’ve killed you 3 times"];
// Generate new random index to select
newQuote = Math.floor(Math.random() * quotes.length);
}
// Each time the button with class 'btn' is clicked, generate a new quote
// and change the output HTML
$(".btn").on("click", function() {
// Change quote values
generator();
// Output changes
$('#output').html(quotes[newQuote]);
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<button class="btn">button</button>
<div id="output"></div>
</body>
</html>
答案 2 :(得分:0)
我认为第一个问题是您错过了数组的名称......
var newQuote = quotes[Math.floor(Math.random() * quotes.length)];
第二件事是该函数似乎并不真正必要,你应该返回一个值,例如
return quotes[Math.floor(Math.random() * quotes.length)];
或根本就不使用它......
$(document).ready(function() {
var quotes = ["Never make permanent decisions", "Knowledge is having the right answer. Intelligence is asking the right question", "I am Strong, Because i’ve been weak", "Scientists have discovered a food that diminishes a woman’s sex drive by 90%…", "I may look calm, but in my head I’ve killed you 3 times"];
$(".btn").on("click", function() {
var newQuote = quotes[Math.floor(Math.random() * quotes.length)];
$('#output').html(newQuote);
});
});
编辑: 以下是函数返回值的示例。
$(document).ready(function() {
function generateQuote() {
var quotes = ["Never make permanent decisions", "Knowledge is having the right answer. Intelligence is asking the right question", "I am Strong, Because i’ve been weak", "Scientists have discovered a food that diminishes a woman’s sex drive by 90%…", "I may look calm, but in my head I’ve killed you 3 times"];
return quotes[Math.floor(Math.random() * quotes.length)];
}
$(".btn").on("click", function() {
$('#output').html(generateQuote());
});
});
答案 3 :(得分:0)
试试这个:
$(document).ready(function() {
var quotes = ["Never make permanent decisions", "Knowledge is having the right answer. Intelligence is asking the right question", "I am Strong, Because i’ve been weak", "Scientists have discovered a food that diminishes a woman’s sex drive by 90%…", "I may look calm, but in my head I’ve killed you 3 times"];
$(".btn").on("click", function() {
$('#output').html(quotes[Math.floor(Math.random() * quotes.length)]);
});
});
我删除生成器函数的原因是如果你保留它,那么var引号将是该函数的本地。不需要它。
最后一个关键是每次点击按钮时生成随机索引。
答案 4 :(得分:0)
答案 5 :(得分:-1)
$(document).ready(function() {
generator();
function generator() {
var quotes = ["Never make permanent decisions", "Knowledge is having the right answer. Intelligence is asking the right question", "I am Strong, Because i’ve been weak", "Scientists have discovered a food that diminishes a woman’s sex drive by 90%…", "I may look calm, but in my head I’ve killed you 3 times"];
var newQuote = [Math.floor(Math.random() * quotes.length)]
}
$(".btn").on("click", function() {
$('#output').html(newQuote.toString());
});
});