【问题标题】:I'm trying to make function randomly choose between the 2 images but nothing happens我正在尝试使功能在两张图像之间随机选择,但没有任何反应
【发布时间】:2022-01-08 00:46:36
【问题描述】:

2张图片上传到编码程序中

Function getFoto() {
  var Foto = new Array();
Foto[0] = "image1.jpg";
Foto[1]=  "image2.jpg";

var Nummer =  Math.floor(Math.random()*Foto.length);
  return document.getElementById("result").innerHTML = '<img src="'+Foto[Nummer]+'" />';}   
getFoto()

【问题讨论】:

    标签: javascript image random


    【解决方案1】:

    您将function关键字设置为不会被JS执行和解析的Function。

    function getFoto() {
      var Foto = new Array();
    Foto[0] = "image1.jpg";
    Foto[1]=  "image2.jpg";
       
    var Nummer =  Math.floor(Math.random()*Foto.length);
    console.log(Nummer)
      return document.getElementById("result").innerHTML = '<img src="'+Foto[Nummer]+'" />';
      }   
    getFoto()
    &lt;div id=result&gt;&lt;/div&gt;

    【讨论】:

      【解决方案2】:

      与编程中的所有事情一样,学习调试本质上是解决大多数问题所需的主要技能。所以在这种情况下,我会首先在 vscode 中设置调试,然后查看你在 math.Floor 函数中传递的值。然后看Nummer的值。然后确保 Foto[Nummer 的值] 有效。

      【讨论】:

        猜你喜欢
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        • 2021-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多