使用数组进行onclick工作

时间:2017-11-29 06:19:05

标签: javascript arrays function onclick

我正在尝试为类创建一个简单的脚本。我已经想出了如何单独执行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>

2 个答案:

答案 0 :(得分:0)

这样的东西?还可以通过addEventListener函数添加事件监听器。

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

var fortune = document.getElementById("fortune");
var button = document.getElementById("button");

button.addEventListener('click', function () {
   var c = Math.round(Math.random() * 9);
   fortune.innerHTML = fortunes[c];
});
&#13;
<button id="button">Get your fortune!</button>
<p id="fortune"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这段代码。

&#13;
&#13;
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."; 

function myFunction() {
  c = Math.round(Math.random()*9);
  //document.write(fortune[c]);
  document.getElementById("fortune").innerHTML = fortune[c] ;
}
&#13;
<button onclick="myFunction()">Get your fortune!</button>
<p id="fortune"></p>
&#13;
&#13;
&#13;