【问题标题】:Call fancybox once AJAX content loaded加载 AJAX 内容后调用 fancybox
【发布时间】:2013-04-13 03:59:02
【问题描述】:

我正在将内容(通过 Ajax)动态加载到名为 <div id="displayinformation"><div>

某些内容的格式设置为,如果有链接,则会出现 Fancybox 灯箱。

我已经测试了 Fancybox,如果内容没有从 Ajax 加载,它就可以工作。但是从 Ajax 加载,它什么也不做。

我相信这可能与:$(document).ready(function() { 有关,但我不确定。

我想我需要在加载 ajax 内容后运行灯箱的代码。

我的代码如下,如果有任何见解,将不胜感激。亲切的问候,保罗。

来电:

<a href="http://player.vimeo.com/video/61692184" title="Lower Title Text" rel="displayvideo" >XX</a>

顶级JS:

<script type="text/javascript">
$(document).ready(function() {

    $("a[rel=displayvideo]").fancybox({
        'overlayShow' : true,
        'width' : 800,
        'height' : 450,
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic',
        'titlePosition' : 'inside',
        'type' : 'iframe',
        'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
        return '<span id="fancybox-title-inside">Video:' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
    }
});

</script>

【问题讨论】:

  • 您似乎使用的是 fancybox v1.3.4(因为您的 API 选项),并且该版本不支持动态添加的元素。检查stackoverflow.com/a/9084293/1055987 的解决方法(包括演示)
  • 看点@JFK。我查看了您放置的 Stackoverflow 链接,这解决了我的问题。太感谢了!说真的,你的帮助是无价的。那里有点疯狂。

标签: javascript jquery ajax fancybox


【解决方案1】:

当您在 Ajax 调用中获得响应时,您需要在该位置放置此代码。

例如

$.post("/index.php", {'param'   : param},
            function(response) {
                //here you need to put fancybox code
                $("a[rel=displayvideo]").fancybox({
                  'overlayShow'   : true,
                  'width'         : 800,
                  'height'            : 450,
                  'transitionIn'      : 'elastic',
                  'transitionOut'     : 'elastic',
                  'titlePosition'     : 'inside',
                  'type' : 'iframe',
                  'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
                   return '<span id="fancybox-title-inside">Video:' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
               }
               });
            });

【讨论】:

  • 您好 Kautil,感谢您的回复,但仍有问题。我完全按照你说的做了,并将调用放在调用内容的ajax php页面中(不是顶级),它不会工作。
  • 您需要将此代码放入 Ajax 调用函数中以获取响应。
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 2011-04-27
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多