sto creando diverse immagini in modo dinamico utilizzando il seguente codice:JQuery - Aggiungi onclick al generati dinamicamente tag img
function refresh_gallery(galleryidentifier, albumid) {
$.ajax({ type: "POST", url: "/Photos/Thumbnails/" + albumid + "/", data: {}, success: function(msg) {
try {
var fotos = eval(msg); $(galleryidentifier).empty(); if (fotos.length == 0) { $(galleryidentifier).html("Press "Add files..." and select files to upload!"); return true; }
for (var f in fotos) {
//this image needs the onclick eventhandler
$(document.createElement("img")).attr({ src: '/images/delete.gif', title: 'Delete ' + fotos[f].Title }).addClass("icon_delete").appendTo(galleryidentifier); ;
$(document.createElement("img")).attr({ src: fotos[f].ThumbnailPath, title: fotos[f].Title }).addClass("thumbnail").appendTo(galleryidentifier);
}
var del_div = $(document.createElement("div")).css({ "padding": "4px" }).appendTo(galleryidentifier);
var delete_span = $(document.createElement("span")).click(delete_files(albumid)).css({ "cursor": "pointer", "font-size": "12px" }).appendTo(del_div);
$(document.createElement("img")).attr({ "src": "/Content/images/delete.png" }).appendTo(delete_span);
$(document.createTextNode("delete all")).appendTo(delete_span);
return true;
}
catch (e) {
alert(e);
}
alert("Refresh error!");
}, error: function() { alert("Refresh error!"); }
});
}
La generazione delle immagini sta lavorando bene, ma voglio aggiungere un EventHandler onclick al primo immagine generata (vedi commento nel codice). Come faccio a fare questo? Sono abbastanza nuovo per JQuery.
Grazie!
Questo sembra davvero bello! – Peter
IMHO {} è un oggetto JSON .. –
@pankysharma Questa è solo la normale notazione di oggetti javascript. JSON è basato su questo modello di notazione. – hitautodestruct