问题
I want to use this library: http://lokeshdhakar.com/projects/lightbox2/
I can't attach rel="lightbox"
to each image so I want to use jQuery to trigger the lightbox.
I was thinking about something like:
$('img').click(function(){
//triger lightbox for this image
//use self src as href
});
How can I trigger the lightbox for one image?
回答1:
Solved the problem by wrapping the img
tag in an a
tag and triggering click on a after that.
$('img').click(function () {
$(this).wrap('<a href="' + $(this).attr("src") + '" rel="lightbox" />');
$(this).parent('a').trigger('click');
});
回答2:
That plugin has to have the rel="lightbox"
to work.
That's mentioned on the How to use
You can add that attribute with jquery using
$('img').attr('rel', 'lightbox');
You will need to add that line inside your document ready function.
If that still doesn't work for you then I'd suggest you use this other plugin
http://fancyapps.com/fancybox/
来源:https://stackoverflow.com/questions/10820277/trigger-lightbox-with-javascript