简体   繁体   English

如何处理:单击图像时,使用 Javascript 生成的相同图像

[英]How to handle: When image is clicked identical image generated using Javascript

The code below works to make one copy of the image clicked.下面的代码用于制作单击的图像的一个副本。 I am needing the code below to work for multiple images.我需要下面的代码来处理多个图像。

For example if I click 5 images on the screen, the same 5 images should generate at the bottom of the page.例如,如果我单击屏幕上的 5 个图像,则页面底部应生成相同的 5 个图像。 The user should be able to select 5 out of 10 images.用户应该能够从 10 张图像中选择 5 张。 Any thoughts?有什么想法吗? Thanks!谢谢!

JS: JS:

function changeImage() {
      var imgSrc = 'http://placehold.it/150';

      if (document.getElementById('myImage').src === imgSrc) {
        document.getElementById('new').src = imgSrc;
      }
    }

HTML HTML

<a href="#" onClick="changeImage()">
  <img id="myImage" src="http://placehold.it/150"></a>
<img id="new" src="http://placehold.it/200">

You can use JQuery clone() function您可以使用 JQuery clone()函数

 $('img').click(function() { $('body').append($(this).clone()); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="http://placehold.it/50x50"> <img src="http://placehold.it/50x50/000000/ffffff"> <img src="http://placehold.it/50x50/1C90F3/ffffff"> <img src="http://placehold.it/50x50/F2BB7C/ffffff">

you can use jquery to do that你可以使用 jquery 来做到这一点

HTML HTML

<a href="#" id="my-image">
  <img id="myImage" src="http://placehold.it/150"></a>
<img id="new" src="http://placehold.it/200" />

JS JS

$('#my-image').click(function(){
   //setting attribute src to be equal as src from #myImage
   $('#new').attr('src', $('#myImage').attr('src'));
   //returning false in order to avoid default action
   return false;
});

So that is it :)就是这样:)

Just remember to put this code after your html or into请记住将此代码放在您的 html 之后或放入

$(document).ready(function(){ ..... });

I hope this helps.我希望这有帮助。

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

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