【发布时间】:2017-10-25 13:18:58
【问题描述】:
对不起,我的格式很糟糕。我希望每次单击按钮时都会显示一个随机报价,而每次单击它时都会显示一个不同的报价。我是否需要在任何地方添加 document.write 或类似的东西?我确定这里和那里有一堆错误,但我只想从函数中返回一个随机引用。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Random quote</title>
<style>
body {
background-color: #fff;
font-family: sans-serif;
color: #28315C;
}
h1 {
text-align: center;
}
button {
border-color: #000;
background-color: #88D0FE;
color: #000;
font-size: 20px;
}
</style>
</head>
<body>
<script>
var quotes = ['this is fortune 1',
'this is fortune 2',
'this is fortune 3',
'this is fortune 4',
'this is fortune 5',
'this is fortune 6',
'this is fortune 7',
];
function newQuote(){
var randomNumber = Math.floor(math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
</script>
</body>
<div id="quoteDisplay">
</div>
<button onclick="newQuote()">New Quote</button>
</html>
【问题讨论】:
-
将
Math.floor(math.random()更改为Math.floor(Math.random() -
调试中的第 1 步 - 浏览器 developer 工具控制台 (F12) - 你的问题是
math!==Math
标签: javascript html css arrays function