使用jQuery在HTML中显示var
Display var in HTML using jQuery
我正在开发免费代码阵营的zipline"构建一个随机报价机"。我尝试过搜索和查看不同的教程,但似乎无法显示我的随机引用。我想我已经接近了,但经过几个小时的尝试,我想我会问专家的!我知道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);
});
});
$(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>
您需要初始化然后设置值。
$(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>
试试这个
var newQuote = quotes[Math.floor(Math.random() * quotes.length)]
您没有看到结果,因为newQuote包含随机报价的索引
试试这个:
$(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)]);
});
});
我删除generator函数的原因是,如果保留它,那么var引号将是该函数的本地引号。不需要它。
最后一个关键是每次单击按钮时生成随机索引。
请在此处找到的工作示例
https://jsfiddle.net/dcxbuaev/
您错误地使用了newQuote
变量
我认为第一个问题是您错过了数组的名称。。。
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());
});
});
$(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());
});
});
相关文章:
- 当var==0时,我如何显示一个警报
- 将我的var显示为HH:mm(javascript)
- JavaScript var 分配一个动态可为空的值显示脚本错误
- var.push 并在 HTML 页面中显示我的新临时信息
- 如何从配置文件中获取$var以显示在模板中
- PHP var 显示在 Chrome Dev Tool 中,但不显示在网页中
- JavaScript JQuery String Var 显示在字段中
- 使用jQuery在HTML中显示var
- 如何在javascript中显示显示var值的警报
- 如果var status = 'Processing',我如何在格式化器中显示删除按钮?
- 在JavaScript中,console.log给出了for循环的每个值.但是var.text只显示最后一个值.这是为什
- jsDocToolkit方法、字段和属性don't在使用self-var时显示
- 如何在html中显示var值为十进制后的两位数字
- 秒表/计时器,保存var并显示在高分列表(HTML, JS)
- 当我使用全局作用域变量没有'var',它显示我错误.为什么
- 如何在Gridview标签中显示javascript var值
- 显示会话var而不刷新页面
- VAR显示从Javascript到PHP显示
- 显示一个DIV, VAR为CLASS
- 改变定时器从var = 1200显示mm:ss的标题