简体   繁体   English

我想在JavaScript中使用onclick更多图片

[英]I want to use onclick more images with javascript

There is one html page and I have a lot of same images. 有一个html页面,我有很多相同的图像。 I want to add alot of same images and when I click one I want it to change. 我想添加很多相同的图像,当我单击一个图像时,我想对其进行更改。 If I have one picture these codes OK! 如果我有一张照片,这些代码可以! but if I have a lot of same pictures How can I do this? 但是如果我有很多相同的图片,该怎么办? Please can you show me a solution way ? 请您告诉我一种解决方法吗?

<html>
<head>
</head>
<style>
#im{
    margin-left:250px;
    margin-right: 0px;

}
</style>
<body>


<script>
function changeImage()
{
element=document.getElementById('im')
if (element.src.match("image"))
  {
  element.src="picture1.png";
  }
else
  {
  element.src="image1.png";
  }
}
</script>
<img id="im" onclick="changeImage()"
src="picture1.png">



</body>
</html>
// store image names here
var images = [
    "picture1.png",
    "picture2.png",
    "picture3.png",
    "picture4.png",
    "picture5.png",
];
// preload images to reduce lag time
var imageArray = [];
for (var i = 0, len = images.length; i < len; i++) {
    imageArray[i] = new Image();
    imageArray[i].src = images[i];
}
// the change function
var index = 0;
function changeImage()
{
    var element = document.getElementById('im');
    element.src = images[index];
    index++;
    if (index === images.length) index = 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM