<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>用this传递当前对象本身</title>
    <meta charset="utf-8" />
</head>
<body>
<img src="usa.gif" onclick="changeImg(this)" alt=""/>
<img src="mexico.gif" onclick="changeImg(this)" alt=""/>

<script>
    var myImages = [
        "usa.gif",
        "canada.gif",
        "jamaica.gif",
        "mexico.gif"
    ];

    function changeImg(that) {
        var newImgNumber = Math.round(Math.random() * 3);

        //确保随机生成的图片索引不和当前的一样
        while (that.src.indexOf(myImages[newImgNumber]) != -1) {
            newImgNumber = Math.round(Math.random() * 3);
        }

        that.src = myImages[newImgNumber];
    }
</script>
</body>
</html>

  

相关文章:

  • 2021-12-02
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2021-04-27
  • 2021-12-22
猜你喜欢
  • 2022-12-23
  • 2021-07-19
  • 2022-01-14
  • 2021-12-15
  • 2022-01-17
  • 2021-11-06
相关资源
相似解决方案