代码:
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 );
});
*/
});
}
我能够动态地可视化图像,但我希望能够在单击图像时(动态地)捕获图片名称。
我知道我必须使用 1. javascript 闭包,2. for 循环 inseatad of for - in 循环。包含图像名称的 JSON 如下所示:
["1.jpg","2.png","3.gif","somename.jpg", "someothername.jpg" .............]
你能帮我解决这个问题吗? 提前致谢!
最佳答案
如果您希望在点击图片时发生某些事情,您可以这样做:
$("img").click(function () {
alert("you clicked "+this.src);
});
这是一个演示:
http://codepen.io/hoganlong/pen/gLleJ
对于 div 命名面板中的项目,您将使用:
$("#panel img").click(function () {
alert("you clicked "+this.src);
});
关于javascript - 闭包,for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14094798/