首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使onclick与数组一起工作

使onclick与数组一起工作
EN

Stack Overflow用户
提问于 2017-11-29 14:19:05
回答 0查看 49关注 0票数 0

我正在试着为类做一个简单的脚本。我已经知道如何分别处理onclick事件和数组,但不确定如何让它们协同工作。我仍然希望命运在刷新时发生变化,但可以选择单击按钮。

Onclick:

代码语言:javascript
复制
<button onclick="myFunction()">Get your fortune!</button>
<p id="fortune"></p>

<script>
function myFunction() {
document.getElementById("fortune").innerHTML = ???? ;
}
</script> 

数组:

代码语言:javascript
复制
<script>
var fortune = new Array(10);
fortune[0]="May life throw you a pleasant curve.";
fortune[1]="Procrastination is the thief of time.";
fortune[2]="Your road to glory will be rocky, but fulfilling.";
fortune[3]="Patience is your alley at the moment. Don’t worry!";
fortune[4]="All things are difficult before they are easy.";
fortune[5]="If you want the rainbow, you have to tolerate the rain.";
fortune[6]="Determination is what you need now.";
fortune[7]="Do not let ambitions overshadow small success.";
fortune[8]="First think of what you want to do; then do what you have to do.";
fortune[9]="Hard words break no bones, fine words butter no parsnips."; 

c=Math.round(Math.random()*9);

document.write(fortune[c]);
</script>
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47546276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档