简体   繁体   English

图像生成器问题。 我如何删除以前的图像?

[英]image generator problems. how do i remove the previous image?

I need help with my project.我的项目需要帮助。 I am new to coding but am learning very quick.我是编码新手,但学得很快。 I have a random image generator but every time I click the button I want the images previously generated to be replaced with the new ones.我有一个随机图像生成器,但每次单击按钮时,我都希望以前生成的图像被新图像替换。 right now the generator just adds two more images every time I click the button.现在,每次我单击按钮时,生成器都会再添加两个图像。 My goal is that every time I click the button two new random images appear taking the place of the two before.我的目标是每次单击按钮时,都会出现两个新的随机图像,以取代之前的两个图像。 Thanks for the help.谢谢您的帮助。 here is my code below but i have redacted the images because i wish to keep them private for now.i put the original puppies in place of the real images.下面是我的代码,但我已经编辑了图像,因为我希望暂时将它们保密。我用原来的小狗代替了真实的图像。

 document.getElementById('button').onclick = function () { //declare an array to store the images var randomImage = []; //insert the URL of images in array randomImage[0] = `https://wi.wallpapertip.com/wsimgs/15-155208_desktop-puppy-wallpaper-hd.jpg`; randomImage[1] = `http://www.petsworld.in/blog/wp-content/uploads/2014/09/running-cute-puppies.jpg`; randomImage[2] = `https://wi.wallpapertip.com/wsimgs/156-1564365_golden-retriever-puppy-desktop-wallpaper-desktop-wallpaper-puppy.jpg`; randomImage[3] = `https://wi.wallpapertip.com/wsimgs/156-1564140_free-puppy-wallpapers-for-computer-wallpaper-cave-cute.jpg`; randomImage[4] = `https://wi.wallpapertip.com/wsimgs/156-1565522_puppies-desktop-wallpaper-desktop-background-puppies.jpg`; randomImage[5] = `https://wi.wallpapertip.com/wsimgs/156-1566650_cute-puppies-desktop-wallpaper-cute-puppies.jpg`; //loop to display five randomly chosen images at once for (let i=0; i< 5; 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="width:150px" />`; } }
 <html> <head> <title> Random Image Generator </title> </head> <body> <center><h2 style="color:green"> Random Image Generator </h2></center> <h4> Click the button to generate and display the five random images: </h4> <!-- call user-defined getRandomImage function to generate image --> <button id="button">Generate Image</button> <br> <br> <span id="result" align="center"> </span> </body> </html>

In order to clear the images before generating a new set, you have to clear the result span element.为了在生成新集之前清除图像,您必须清除result跨度元素。 Also, I adjusted the code below to declare the images in the array and created a reference to the element outside of the function to minimize the tasks being done in the function.此外,我调整了下面的代码以声明数组中的图像并创建了对 function 外部元素的引用,以最大限度地减少 function 中完成的任务。

 //declare an array to store the images var randomImage = new Array(5); var imageHolder = document.getElementById("result"); //insert the URL of images in array randomImage[0] = "https://wi.wallpapertip.com/wsimgs/15-155208_desktop-puppy-wallpaper-hd.jpg"; randomImage[1] = "http://www.petsworld.in/blog/wp-content/uploads/2014/09/running-cute-puppies.jpg"; randomImage[2] = "https://wi.wallpapertip.com/wsimgs/156-1564365_golden-retriever-puppy-desktop-wallpaper-desktop-wallpaper-puppy.jpg"; randomImage[3] = "https://wi.wallpapertip.com/wsimgs/156-1564140_free-puppy-wallpapers-for-computer-wallpaper-cave-cute.jpg"; randomImage[4] = "https://wi.wallpapertip.com/wsimgs/156-1565522_puppies-desktop-wallpaper-desktop-background-puppies.jpg"; randomImage[5] = "https://wi.wallpapertip.com/wsimgs/156-1566650_cute-puppies-desktop-wallpaper-cute-puppies.jpg"; function getRandomImage() { // Clear the imageHolder innerHTML first imageHolder.innerHTML = ""; //loop to display five randomly chosen images at once for (let i = 0; i < 5; 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 imageHolder.innerHTML += '<img src="' + randomImage[number] + '" style = "width:150px" / > '; } }
 <center> <h2 style="color:green"> Random Image Generator </h2> </center> <h4> Click the button to generate and display the five random images: </h4> <!-- call user-defined getRandomImage function to generate image --> <button onclick="getRandomImage()"> Generate Image </button> <br> <br> <span id="result" align="center"> </span>

As @phentnil describes in the answer.正如@phentnil 在答案中所描述的那样。 What you really need to do is to clean your elements before generating the next new ones.你真正需要做的是在生成下一个新元素之前清理你的元素。

imageHoder.innerHTML = ""

Also, be aware of that innerHTML will be one of the main security issue if you do not prevent or check any script injected inside it.此外,请注意,如果您不阻止或检查注入其中的任何脚本,innerHTML 将成为主要的安全问题之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dropzone删除上一张图片 - Dropzone remove previous image 如何使这个随机图像生成器 function 成为页面的背景图像? - How do I make this random image generator function the background image of a page? 如何将新图像拼接到我要删除的图像所在的同一 position 中的数组中并显示此图像 - How do I splice a new image into an array in the same position that the image that I want to remove is in and to display this image 我如何转换内部<script> tag to an external Javascript file? (for random image generator) - How do I convert an internal <script> tag to an external Javascript file? (for random image generator) 我面临两个问题。 此代码应该打印“待办事项”列表 - I am facing two problems. this code supposed to print the 'to do' list 如何在单击时连续获取值并将其传递给 QR 码生成器并弹出图像? [等候接听] - how do i get the values in a row on click and pass it to a QR Code generator and pop up the image? [on hold] 如何在JavaScript中添加和删除图像上的边框 - How do I add and remove a border on image in javascript 如何从图像路径中删除字符串 - How do I remove strings from the path of image 循环后如何删除/隐藏图像? - How do I remove/hide the image after the loop? 如何在错误时删除/隐藏图像容器 - How do I remove/hide image container on error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM