对For循环中的函数数组应用onClick(每次单击按钮时执行的每个函数)
Applying onClick to Function Array in a For Loop (Each Function executed on each Button Click)
斯塔克夫弗洛的人们好!
我希望使用Javascript中的一个按钮按顺序(一个接一个(执行一系列函数。点击按钮,而不是一次全部执行。(请参阅下面的功能显示(
目前,所有功能都同时执行。有人能帮忙吗!?我希望能够点击按钮,让每个彩色方块一个接一个地出现在屏幕上,每次点击按钮。
function red () {
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#red";
ctx.fillRect(20,60,23,23);
}
function blue() {
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#blue";
ctx.fillRect(20,100,23,23);
}
function green() {
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#green";
ctx.fillRect(20,20,23,23);
}
function functionarray() {
var array_of_functions = [
green,
red,
blue,
]
for (i = 0; i < array_of_functions.length; i++) {
array_of_functions[i]();
}
}
function displayNext() {
var array_of_functions = [
green,
red,
blue,
]
for (i = 0; i < array_of_functions.length; i++) {
array_of_functions[i].onclick = array_of_functions[i]();
//array_of_functions[i].onclick();
}
}
<p><button type="button" onclick="displayNext()">On Click, next colour</button></p>
</html>
`
<html>
<head>
<title></title>
</head>
<body>
<p>
<button type="button" id="myID">next color</button>
</p>
<script>
var canvas = document.getElementById("myCanvas"),
ctx = canvas.getContext("2d"),
red = function red() {
ctx.fillStyle="#red";
ctx.fillRect(20,60,23,23);
},
blue = function blue() {
ctx.fillStyle="#blue";
ctx.fillRect(20,100,23,23);
},
green = function green() {
ctx.fillStyle="#green";
ctx.fillRect(20,20,23,23);
},
lastCall = 0,
array_of_functions = [green, red, blue],
displayNext = function displayNext() {
array_of_functions[lastCall]();
lastCall += 1;
};
document.getElementById('myID').addEventListener('click', displayNext);
</script>
</body>
</html>
相关文章:
- Javascript onclick函数会立即调用(而不是在单击时调用)
- Javascript在处理函数时防止单击
- JavaScript-onClick将按钮ID或单击按钮的文本传递给函数
- 使用单击函数中的jQuery切换元素
- 单击函数仅对某些数据名称执行
- 如何在单击、Knockout时传递函数中的值
- 如何创建背景图像列表'URL,并使用jQuery在单击函数时应用它们
- 运行“;非“;单击另一个函数中的函数仅一次
- 调用函数和单击动画
- 如何在单击组件时调用函数
- 单击从子函数返回父函数
- 调用函数单击选择标签中的选项
- jQuery 函数 - 单击两次返回一个值
- 模式弹出窗口无法正常工作,它多次调用函数 - 单击事件处理程序运行多次
- 在 JSON 解析中添加函数单击
- 调用多个函数单击“反应JS”
- 如何在使用javascript函数单击网格时设置文本框值
- 如何通过使用onClick()函数单击按钮来填充Javascript中的表
- 我如何添加多个值从一个选择列表框到另一个列表框上使用javascript函数单击按钮
- 手动触发函数单击事件对象