如何在javascript中使用相同的图像多次显示相同的图像 [英] how to display same image multiple times using same image in javascript

查看:81
本文介绍了如何在javascript中使用相同的图像多次显示相同的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个页面上多次显示图像(例如:气球)。我知道我可以通过添加此

I want to display an image (for example: balloon) multiple times on a single page. I know I can do it by adding this

<img id="101" src="balloons.png" >

在.html中,因为我需要为每张图片使用不同的ID。但他们所有的src都是一样的。有没有其他办法让我只能加载图像一次,但可以使用我想要的同一图像的多个副本?

in .html as many images as I need using different id for each image. But all of their src will be same. Is there any other way around so that I can load the image only one time but can use as many copies of the same image as I want?

推荐答案

您可以添加for循环并创建图像对象,然后将其附加到正文。

You can add the for loop and create the object of image then append it to the body.

   var img = new Image();
     img.src = "./ImageName.png"
    for (var i = 0; i < 10; i++) {      
        document.body.appendChild(img)
    }

注意:它会给图像10次,因为我添加条件显示图像10次。

Note: It will give the image 10 times because I added the condition to display the images 10 times.

这篇关于如何在javascript中使用相同的图像多次显示相同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆