When i've loaded a page, you'll be able to upload some photos. The images that i've uploaded is then appended (using .append) to a list-element. How can i assign my JS code to make those images clickable?
I've tried with $('img').click() etc but since the images is added after the DOM is ready i dont think they e开发者_如何学编程xactly is available.. how can i fix this?
.live() was made for this:
$('img').live('click', function() {
// code here
});
Check out the jQuery .live()
handler
The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation: a handler bound to an ancestor element is responsible for events that are triggered on its descendants. The handler passed to .live() is never bound to an element; instead, .live() binds a special handler to the root of the DOM tree.
Alternatively, you can check out the jQuery .delegate()
handler and read this StackOverflow response on the differences between .live()
and .delegate()
...
Jquery live() vs delegate()
I hope this helps.
精彩评论