【问题标题】:Random image javascript随机图像 javascript
【发布时间】:2021-04-15 20:20:45
【问题描述】:

尽管我觉得答案很简单,但我对这个随机图像 javascript 有点挣扎。我的代码一次生成四个字母(图像)。如何获取代码以重新生成新字母,而不是添加四个额外的字母(图像)?

这里还有一个Jsfiddle

  <script>
function getRandomImage() {
//declare an array to store the images
var randomImage = new Array();

//insert the URL of images in array
randomImage[0] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/a.png";
randomImage[1] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/b.png";
randomImage[2] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/c.png";
randomImage[3] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/d.png";
randomImage[4] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/e.png";
randomImage[5] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/f.png";
randomImage[6] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/g.png";
randomImage[7] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/h.png";
randomImage[8] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/i.png";
randomImage[9] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/j.png";
randomImage[10] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/k.png";
randomImage[11] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/l.png";
randomImage[12] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/m.png";
randomImage[13] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/n.png";
randomImage[14] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/o.png";
randomImage[15] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/p.png";
randomImage[16] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/q.png";
randomImage[17] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/r.png";
randomImage[18] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/s.png";
randomImage[19] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/t.png";
randomImage[20] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/u.png";
randomImage[21] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/v.png";
randomImage[22] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/w.png";
randomImage[23] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/x.png";
randomImage[24] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/y.png";
randomImage[25] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/z.png";
//loop to display five randomly chosen images at once
for (let i=0; i< 4; i++) {
//generate a number and provide to the image to generate randomly
var number = Math.floor(Math.random()*randomImage.length);
//print the images generated by a random number
document.getElementById("result").innerHTML += '<img src="'+ randomImage[number] +'" style="height:150px";/>';
}
}

</script>
<body>
<h1> GENERATE YOUR LETTERS... </h1>
<!-- call user-defined getRandomImage function to generate image -->  
<center><button onclick = "getRandomImage()" class="btn btn-white btn-        animate">Let's Go!</button></center>
<br> <br>
<span id="result" align="center"> </span> 

【问题讨论】:

    标签: javascript html image random generate


    【解决方案1】:

    您可以在 for 循环之前添加 document.getElementById('result').innerHTML = "" 以在添加 4 个新项目之前清除结果 div。

    您还可以通过使用 for 循环来生成图像 URL,从而大大减少您的代码。

    var letters = 'abcdefghijklmnopqrstuvwxyz'.split('');
    
    for (let i = 0; i < letters.length; i++) {
        randomImage.push(`http://www.englishclass.dk/_themes/englishclass/img/scrabble/${letters[i]}.png`)
    }
    

    【讨论】:

    • 你能把它加到我的代码中吗?我应该用您的“var letters”替换所有网址吗?抱歉,JS 还是让我很困惑
    • 是的,这是您的 JSFiddle 的一个工作分叉示例。 jsfiddle.net/s29h8q5w
    • 你可以写成更短的const randomImage = Array.from('abcdefghijklmnopqrstuvwxyz').map(letter =&gt; `http://www.englishclass.dk/_themes/englishclass/img/scrabble/${letter}.png`);
    【解决方案2】:

    document.getElementById('result').innerHTML = " " 在 for 循环之前

        <script>
    function getRandomImage() {
    //declare an array to store the images
    var randomImage = new Array();
    
    //insert the URL of images in array
    randomImage[0] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/a.png";
    randomImage[1] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/b.png";
    randomImage[2] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/c.png";
    randomImage[3] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/d.png";
    randomImage[4] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/e.png";
    randomImage[5] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/f.png";
    randomImage[6] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/g.png";
    randomImage[7] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/h.png";
    randomImage[8] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/i.png";
    randomImage[9] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/j.png";
    randomImage[10] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/k.png";
    randomImage[11] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/l.png";
    randomImage[12] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/m.png";
    randomImage[13] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/n.png";
    randomImage[14] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/o.png";
    randomImage[15] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/p.png";
    randomImage[16] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/q.png";
    randomImage[17] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/r.png";
    randomImage[18] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/s.png";
    randomImage[19] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/t.png";
    randomImage[20] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/u.png";
    randomImage[21] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/v.png";
    randomImage[22] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/w.png";
    randomImage[23] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/x.png";
    randomImage[24] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/y.png";
    randomImage[25] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/z.png";
    //loop to display five randomly chosen images at once
    document.getElementById("result").innerHTML="";
    for (let i=0; i< 4; i++) {
    //generate a number and provide to the image to generate randomly
    var number = Math.floor(Math.random()*randomImage.length);
    //print the images generated by a random number
    
    document.getElementById("result").innerHTML += '<img src="'+ randomImage[number] +'" style="height:150px";/>';
    }
    }
    
    </script>
    <body>
    <h1> GENERATE YOUR LETTERS... </h1>
    <!-- call user-defined getRandomImage function to generate image -->  
    <center><button onclick = "getRandomImage()" class="btn btn-white btn-        animate">Let's Go!</button></center>
    <br> <br>
    <span id="result" align="center"> </span> 
    

    【讨论】:

    • 在循环之前你必须添加 document.getElementById("result").innerHTML=""; for (let i=0; i
    【解决方案3】:

    在函数定义的开头添加以下代码:

    document.getElementById("result").innerHTML = "";
    

    像这样

    <script>
    function getRandomImage() {
    
    document.getElementById("result").innerHTML = "";
    
    //declare an array to store the images
    var randomImage = new Array();
    
    //insert the URL of images in array
    randomImage[0] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/a.png";
    randomImage[1] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/b.png";
    randomImage[2] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/c.png";
    randomImage[3] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/d.png";
    randomImage[4] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/e.png";
    randomImage[5] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/f.png";
    randomImage[6] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/g.png";
    randomImage[7] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/h.png";
    randomImage[8] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/i.png";
    randomImage[9] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/j.png";
    randomImage[10] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/k.png";
    randomImage[11] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/l.png";
    randomImage[12] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/m.png";
    randomImage[13] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/n.png";
    randomImage[14] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/o.png";
    randomImage[15] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/p.png";
    randomImage[16] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/q.png";
    randomImage[17] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/r.png";
    randomImage[18] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/s.png";
    randomImage[19] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/t.png";
    randomImage[20] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/u.png";
    randomImage[21] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/v.png";
    randomImage[22] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/w.png";
    randomImage[23] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/x.png";
    randomImage[24] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/y.png";
    randomImage[25] = "http://www.englishclass.dk/_themes/englishclass/img/scrabble/z.png";
    //loop to display five randomly chosen images at once
    for (let i=0; i< 4; i++) {
    //generate a number and provide to the image to generate randomly
    var number = Math.floor(Math.random()*randomImage.length);
    //print the images generated by a random number
    document.getElementById("result").innerHTML += '<img src="'+ randomImage[number] +'" style="height:150px";/>';
    }
    }
    
    </script>
    <body>
    <h1> GENERATE YOUR LETTERS... </h1>
    <!-- call user-defined getRandomImage function to generate image -->  
    <center><button onclick = "getRandomImage()" class="btn btn-white btn-        animate">Let's Go!</button></center>
    <br> <br>
    <span id="result" align="center"> </span> 
    

    【讨论】:

      【解决方案4】:

      正如"innerHTML += ..." vs "appendChild(txtNode)" 中所述,通过执行innerHTML += '&lt;img /&gt;' 添加新元素会导致浏览器重建整个容器。这可能会导致性能问题,具体取决于您调用它的位置、时间和频率。为了完整起见,我提供了性能更高的示例(更冗长但通常更好的做法)。

      这应该替换您的 for 循环:

      // Clear all childs from the container
      
      const container = document.getElementById("result");
      container.childNodes.forEach((child) => child.remove());
      
      for (let i = 0; i < 4; i++) {
        const number = Math.floor(Math.random() * randomImage.length);
      
        // Create a new <img /> element and append it to the container
      
        const image = document.createElement("img");
        image.src = randomImage[number];
        image.style.height = "150px";
        container.append(image);
      }
      

      【讨论】:

        【解决方案5】:

        您也可以通过使用最高投票答案here 中给出的方法,在不拆分字符串的情况下获得一个随机字母。然后,您可以像上面其他人提到的那样将您的代码简化为几行。

        我已经注释了下面的代码。


        // Function triggered with onclick event
        function getRandomImage() {
        
          // Clear the previous letters
          document.getElementById("result").innerHTML = "";
        
          // Loop
          for (let i = 0; i < 4; i++) {
        
            // Generate random letter
            letter = String.fromCharCode(97 + Math.floor(Math.random() * 26))
        
            // Add image to #result
            document.getElementById("result").innerHTML += '<img src="http://www.englishclass.dk/_themes/englishclass/img/scrabble/' + letter + '.png" style="height:150px";/>';
        
          }
        
        }
          <h1> GENERATE YOUR LETTERS... </h1>
          <center>
            <button onclick="getRandomImage()" class class="btn btn-white btn-animate">
              Let's Go!
            </button>
          </center>
          <br>
          <br>
         <span id="result" align="center"> </span>

        【讨论】:

          猜你喜欢
          • 2021-06-28
          • 2013-11-10
          • 1970-01-01
          • 1970-01-01
          • 2017-04-23
          • 2021-01-18
          • 2021-08-17
          • 2013-03-06
          • 1970-01-01
          相关资源
          最近更新 更多