我正在试着为类做一个简单的脚本。我已经知道如何分别处理onclick事件和数组,但不确定如何让它们协同工作。我仍然希望命运在刷新时发生变化,但可以选择单击按钮。
Onclick:
<button onclick="myFunction()">Get your fortune!</button>
<p id="fortune"></p>
<script>
function myFunction() {
document.getElementById("fortune").innerHTML = ???? ;
}
</script> 数组:
<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>https://stackoverflow.com/questions/47546276
复制相似问题