简体   繁体   English

闭包,用于循环

[英]closures, for loop

the code: 编码:

function updateProfil() {
  $.getJSON("./index_logg2.php", null, processCustom); 
}

function processCustom(data) {
  $.each(data, function(k, v) {
    $(".panel").append('<center><img src="images/custom/'+ v +' "title="Click to set it" "></center><br />');
    /*
    $(".panel").click(function() {
      var data= ???
      $.post("./index_logg2.php", { data: ??? }, updateProfil ); 
    });
    */
  });
}

I am able to dynamically visualize the images, but i want to be able to capture (dynamically) the picture names when the image is clicked. 我能够动态地可视化图像,但是我希望能够在单击图像时捕获(动态)图片名称。

I know I have to use 1. javascript closures, 2. for loop inseatad of for - in loop. 我知道我必须使用1. javascript闭包,2. for循环inseatad for-in循环。 JSON containing image names looks like: 包含图像名称的JSON如下所示:

["1.jpg","2.png","3.gif","somename.jpg", "someothername.jpg" .............]

will you help me with this please. 您能帮我这个忙吗? Thank you in advance ! 先感谢您 !

When you want something to happen when an image is clicked on you can do something like this: 当您希望单击图像时发生某些事情时,可以执行以下操作:

   $("img").click(function () {
       alert("you clicked "+this.src);
   });

Here is a demo: 这是一个演示:

http://codepen.io/hoganlong/pen/gLleJ http://codepen.io/hoganlong/pen/gLleJ

For just the items in a div named panel you would use: 仅对div命名面板中的项目使用:

   $("#panel img").click(function () {
       alert("you clicked "+this.src);
   });

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

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