【问题标题】:Random images on click (three times)点击随机图片(三遍)
【发布时间】:2016-08-06 19:24:47
【问题描述】:

我制作了一个包含三个图像(头部、躯干和腿)的简单页面。按下三个按钮(头、躯干、腿),其中一个随机变化:http://matti.tv/VRT/

我的问题:头部随机出现,没有缺陷,但躯干和腿出现错误(“未定义”)。我试图修复它,但我不是专家,我的代码非常混乱。

我按照earlier topic 中关于单击按钮时随机图像的提示进行操作,但使用两个额外的随机图像生成器似乎我做错了,我就是想不通。

我知道我应该将我的三个 myImage 放在一个函数中,但是我以后如何在 img 和按钮中调用它们?

<script>
    function imgchange1() {    
        var myImages1 = new Array();
        myImages1[1] = "1.jpg";
        myImages1[2] = "2.jpg";        
        var rnd = Math.floor(Math.random() * myImages1.length);
        if (rnd == 0) {
            rnd = 1;
        }    
        document.getElementById("gen-img1").src = myImages1[rnd];    
    }

    function imgchange2() {    
        var myImages2 = new Array();
        myImages2[19] = "19.jpg";
        myImages2[20] = "20.jpg";
        myImages2[21] = "21.jpg";

        var rnd = Math.floor(Math.random() * myImages2.length);
        if (rnd == 0) {
            rnd = 1;
        }    
        document.getElementById("gen-img2").src = myImages2[rnd];    
    }

    function imgchange3() {    
        var myImages3 = new Array();
        myImages3[46] = "46.jpg";
        myImages3[47] = "47.jpg";
        myImages3[48] = "48.jpg";

        var rnd = Math.floor(Math.random() * myImages3.length);
        if (rnd == 0) {
            rnd = 1;
        }    
        document.getElementById("gen-img3").src = myImages3[rnd];    
    }

</script>
<center>
    <p><img id="gen-img1" src="7.jpg"></p>    
    <p><img id="gen-img2" src="20.jpg"></p>        
    <p><img id="gen-img3" src="46.jpg"></p>    
</center>    
<center>
    <p>            
        <input type="button" value="Hoofd" onclick="imgchange1();" />
        <input type="button" value="Torso" onclick="imgchange2();" />
        <input type="button" value="Benen" onclick="imgchange3();" />
    </p>        
</center>

【问题讨论】:

  • 请阅读minimal reproducible example,并特别注意底部调试小程序的链接。
  • 你有没有注意到,在你提到的另一个问题上,据说你应该使用“'myImages1[rnd]'”;在双引号内,然后是单引号
  • 如果你可以为你的代码发布任何 plunker 会更好,你可以在线使用任何图像 url

标签: javascript image random onclick


【解决方案1】:

那是因为您将项目推送到您的 myImages2mymyImages3,索引分别从 1946 开始,但它必须从 1 开始。

myImages2[1]="19.jpg";
myImages2[2]="20.jpg";
.
.

&

myImages3[1]="46.jpg";
myImages3[2]="19.jpg";
.
.
.

【讨论】:

    【解决方案2】:

    在每个函数中,随机值的范围从 0 到数组长度,但是在存储 src 时,它就像 arr[17]、arr[18]

    请检查下面的sn-p:

    function imgchange1() {
    
      var myImages1 = new Array();
      myImages1[1] = "http://www.logologo.com/logos/flower-logo.jpg";
      myImages1[2] = "https://www.logobee.com/uploads/free-logo1.jpg";
      var rnd = Math.floor(Math.random() * myImages1.length);
      if (rnd == 0) {
        rnd = 1;
      }
      document.getElementById("gen-img1").src = myImages1[rnd];
    }
    
    function imgchange2() {
    
      var myImages2 = new Array();
      myImages2[1] = "https://image.freepik.com/free-vector/abstract-and-colorful-logo_23-2147504722.jpg";
      myImages2[2] = "http://www.logospike.com/wp-content/uploads/2016/03/Free_Logo_03.jpg";
      var rnd = Math.floor(Math.random() * myImages2.length);
      if (rnd == 0) {
        rnd = 1;
      }
      console.log(myImages2[rnd]);
      document.getElementById("gen-img2").src = myImages2[rnd];
    
    }
    
    function imgchange3() {
    
      var myImages3 = new Array();
      myImages3[1] = "http://www.logologo.com/logos/globe-swooshes-logo.jpg";
      myImages3[2] = "https://image.freepik.com/free-vector/infinite-logo_23-2147515990.jpg";
      var rnd = Math.floor(Math.random() * myImages3.length);
      if (rnd == 0) {
        rnd = 1;
      }
    
      document.getElementById("gen-img3").src = myImages3[rnd];
    
    }
    <center>
      <p>
        <img id="gen-img1" src="http://www.logologo.com/logos/flower-logo.jpg">
      </p>
    
      <p>
        <img id="gen-img2" src="https://image.freepik.com/free-vector/abstract-and-colorful-logo_23-2147504722.jpg">
      </p>
    
      <p>
        <img id="gen-img3" src="http://www.logologo.com/logos/globe-swooshes-logo.jpg">
      </p>
    
    </center>
    
    <center>
      <p>
    
        <input type="button" value="Hoofd" onclick="imgchange1();" />
        <input type="button" value="Torso" onclick="imgchange2();" />
        <input type="button" value="Benen" onclick="imgchange3();" />
      </p>
    
    </center>

    【讨论】:

    • 嘿@Meurisse 我创建了一个虚拟代码,每个插槽有两个图像。如果您有任何困惑,请告诉我。但是尝试在这里更改数据结构。图像应该在某些数据结构中存储一次。在点击回调中,只生成随机数,并获取该索引对应的图像。每次创建具有相同图像 src 的单独数组时都是性能打击步骤。请避免这种情况。
    猜你喜欢
    • 2012-02-13
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多