javascript - 更改按钮图像 onclick

标签 javascript replace

我有这段代码可以从图像 1 更改为图像 2。如何才能使其从图像 2 更改为图像 3,然后返回图像 1?谢谢

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()"><img id="myImg" src="image1.gif" width="107"     height="98"></button>

<script>
function myFunction()
{
document.getElementById("myImg").src="image2.gif";
}
</script>

</body>
</html>

最佳答案

http://jsfiddle.net/9R2J7/1/

var img_array = ['http://placehold.it/100x100/green', 'http://placehold.it/100x100/blue', 'http://placehold.it/100x100/red'];
i = 0;

function myFunction() {
    i++;
    document.getElementById("myImg").src = img_array[i];
    if (i == img_array.length - 1) {
        i = -1;

    }

}

关于javascript - 更改按钮图像 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23462786/

相关文章:

javascript - 如何使用discord.js 关闭打开的消息收集器

javascript - 从数据库字段中提取值到复选框值 PHP

javascript - 在二维数组js中选择对象

javascript - typeof ("2px"- 1 ) 返回数字, "2px"- 1 返回 NaN。为什么?

javascript - 从 JSON 获取数据到 D3.js

javascript - 根据链接文本的内容添加类

python - 如何将一列拆分为一些列,其中包含像 dict 类型的单元格?

php - 从字符串中删除列入黑名单的字词,然后消除不必要的空格

python - Pandas 如何通过正则表达式从列提取到多行?

javascript - jQuery : Replace all unformatted text outside of html tags with multiple dropdown boxes