简体   繁体   English

AJAX加载了lightbox2的内容

[英]AJAX loaded content with lightbox2

I'm using the Lightbox2 script and the content of my page is loaded via an AJAX request. 我正在使用Lightbox2脚本,我的页面内容是通过AJAX请求加载的。
I don't find any function to attach new images or initialize Lightbox2 after an AJAX request, How can I make that in order to use Lightbox2 for images loaded via AJAX ? 在AJAX请求之后,我找不到附加新图像或初始化Lightbox2的任何功能,我如何才能使用Lightbox2来处理通过AJAX加载的图像?

Léo 莱奥

I had to reinitialize lightbox in order to detect new images. 我不得不重新初始化灯箱以检测新图像。 I have done that like this: 我这样做了:

window.lightbox.init();

I placed this code in my success handler of ajax call after I have added the new content: 在添加新内容后,我将此代码放在ajax调用的成功处理程序中:

contentContainer.empty().html(data);

From the documentation files: http://lokeshdhakar.com/projects/lightbox2/ You do not need to initiate anything, any image link with the data-lightbox attribute like this: 从文档文件: http//lokeshdhakar.com/projects/lightbox2/您不需要启动任何内容,任何与data-lightbox属性的图像链接如下:

<a href="img/image-1.jpg" data-lightbox="image-1" data-title="My caption">Image #1</a>

will work automagically even if loaded by AJAX as soon as you add it to your DOM. 即使在将AJAX添加到DOM后由AJAX加载,它也会自动运行。

EDIT: After having taken a look at the lightbox script, you may have to change as well the line #51: 编辑:看了灯箱脚本后,你可能还需要更改第51行:

$('body').on('click', 'a[rel^=lightbox], area[rel^=lightbox], a[data-lightbox], area[data-lightbox]', function(event) {

into this: 进入这个:

$('body').live('click', 'a[rel^=lightbox], area[rel^=lightbox], a[data-lightbox], area[data-lightbox]', function(event) {

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

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