如何将 fancybox 绑定到动态添加的元素?

发布于 2024-12-29 23:22:48 字数 398 浏览 3 评论 0原文

我使用 jquery fancybox 1.3.4 作为弹出表单。

但我发现 fancybox 无法绑定到动态添加的元素。例如,当我向当前文档添加 html 元素时。

像这样: 首先,我使用 jquery 将一个元素附加到主体,

  $(document.body).append("<a href="home/index" class="fancybox"/>");

并调用 fancybox,

  $(".ajaxFancyBox").fancybox({padding: 0});

但 fancybox 不适用于动态添加的元素。

我不能从这个元素调用 fancybox 吗?

I use jquery fancybox 1.3.4 as pop form.

but I found fancybox can't bind to element dynamic added. for example when I add a html element to current document.

like this:
first I append a element to body use jquery,

  $(document.body).append("<a href="home/index" class="fancybox"/>");

and I call fancybox,

  $(".ajaxFancyBox").fancybox({padding: 0});

but fancybox don't work with dynamic added element.

and I can't call fancybox from this element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

和我恋爱吧 2025-01-05 23:22:48

将 fancybox (v1.3.x) 绑定到动态添加的元素的最简单方法是:

1: 升级到 jQuery v1.7.x(如果还没有)

2: 使用 jQuery API on() + focusin 事件。

为了使其工作,您需要根据上面的代码使用 class="ajaxFancyBox" 找到元素的 parent 元素(或 parentparent ,因为你需要它)并将 jQuery on() 附加到它,例如,有这个 html:

<div id="container">
 <a class="ajaxFancyBox" href="image01.jpg">open image 01</a>
 <a class="ajaxFancyBox" href="image02.jpg">open image 02</a>
</div>

.. 我们将应用 on()< /code> 和 focusin 事件到元素id="container"parent)如上例所示,例如:

$("#container").on("focusin", function(){
 $("a.ajaxFancyBox").fancybox({
  // fancybox API options here
  'padding': 0
 }); // fancybox
}); // on

您可以根据需要应用任何 fancybox 选项。此外,您可能对不同类型的内容(在 on() 方法内)使用不同的脚本,例如:

$("#container").on("focusin", function(){
 $("a.ajaxFancyBox").fancybox({
  // fancybox API options here
  'padding': 0
 }); // fancybox
 $("a.iframeFancyBox").fancybox({
  // fancybox API options here
  'padding': 0,
  'width': 640,
  'height': 320,
  'type': 'iframe'
 }); // fancybox
}); // on

重要:上面的示例在 Chrome 上的工作方式不同。 解决方法是将 tabindex 属性添加到绑定到 fancybox 的所有元素中,这样

<div id="container">
 <a tabindex="1" class="ajaxFancyBox" href="image01.jpg">open image 01</a>
 <a tabindex="1" class="ajaxFancyBox" href="image02.jpg">open image 02</a>
</div>

可以解决问题,并且可以在包括 IE7+ 在内的大多数浏览器中工作(据我所知) 。

在此处查看我的演示页面

更新:2012 年 3 月 7 日。

我被告知这该方法仅在您向页面添加新内容时有效,但在替换页面内容时无效。

该方法实际上适用于上述两种情况。只需确保新的替换内容也加载到应用 .on() 方法的容器内即可。

查看演示

Chrome 的 tabindex 解决方法也适用。

The easiest way to bind fancybox (v1.3.x) to dynamically added elements is:

1: Upgrade to jQuery v1.7.x (if you haven't yet)

2: Set your script using jQuery API on() + the focusin event.

To make it work you need to find the parent element of your elements with class="ajaxFancyBox" as per your code above (or the parent of the parent as you need it) and attach jQuery on() to it so for example, having this html:

<div id="container">
 <a class="ajaxFancyBox" href="image01.jpg">open image 01</a>
 <a class="ajaxFancyBox" href="image02.jpg">open image 02</a>
</div>

.. we will apply on() and focusin event to the element with id="container" (the parent) as in the example above, like:

$("#container").on("focusin", function(){
 $("a.ajaxFancyBox").fancybox({
  // fancybox API options here
  'padding': 0
 }); // fancybox
}); // on

You can apply any fancybox option as you need. Additionally you may have different scripts for different type of content (inside the on() method) like:

$("#container").on("focusin", function(){
 $("a.ajaxFancyBox").fancybox({
  // fancybox API options here
  'padding': 0
 }); // fancybox
 $("a.iframeFancyBox").fancybox({
  // fancybox API options here
  'padding': 0,
  'width': 640,
  'height': 320,
  'type': 'iframe'
 }); // fancybox
}); // on

IMPORTANT: the example above won't work like that on Chrome. The workaround is to add the tabindex attribute to all of your elements bound to fancybox like

<div id="container">
 <a tabindex="1" class="ajaxFancyBox" href="image01.jpg">open image 01</a>
 <a tabindex="1" class="ajaxFancyBox" href="image02.jpg">open image 02</a>
</div>

that solves the issue and will work (as far as I know) in most browsers including IE7+.

See my demo page here

UPDATE: March 07, 2012.

I was told that this method only works when you add new content to the page but not when you replace the content of the page.

The method actually works on either of the two scenarios mentioned above. Just make sure that the new replacing content is also loaded inside the container where you applied the .on() method.

See demo

The tabindex workaround for Chrome also applies.

离旧人 2025-01-05 23:22:48

您指定了错误的类名,请尝试以下操作:

$(".fancybox").fancybox({padding: 0});

You're specifying the wrong class name, try this instead:

$(".fancybox").fancybox({padding: 0});
时光暖心i 2025-01-05 23:22:48

你可以尝试这样的事情:

 $(document.body).append("<a href='home/index' class='fancybox' onclick='showFancybox()'/>");

然后创建一个函数来创建并显示 Fancybox:

function showFancybox(){
    $.fancybox(
            '<h2>Hi!</h2><p>Content of popup</p>',
            {
                    'autoDimensions'    : false,
                'width'             : 350,
                'height'            : 'auto',
                'transitionIn'      : 'none',
                'transitionOut'     : 'none'
            }
        );
}

You could try something like this maybe:

 $(document.body).append("<a href='home/index' class='fancybox' onclick='showFancybox()'/>");

And then make a function to create and show Fancybox:

function showFancybox(){
    $.fancybox(
            '<h2>Hi!</h2><p>Content of popup</p>',
            {
                    'autoDimensions'    : false,
                'width'             : 350,
                'height'            : 'auto',
                'transitionIn'      : 'none',
                'transitionOut'     : 'none'
            }
        );
}
他不在意 2025-01-05 23:22:48

正如上面评论中所建议的,我们可以在此类问题中使用以下方法(将元素绑定到动态元素) -

$("#container").on("focusin", function(){

    if($(this).hasClass("fancybox-bind")){                //check if custom class exist 
       return 0;                                          //now fancybox event will not be binded
    }else{                                                //add class to container
        $(this).addClass("fancybox-bind");
    }

    $("a.ajaxFancyBox").fancybox({                        // fancybox API options here
        'padding': 0
    }); // fancybox

   $("a.iframeFancyBox").fancybox({                       // fancybox API options here
     'padding': 0,
     'width': 640,
     'height': 320,
     'type': 'iframe'
   }); // fancybox
}); // on

As suggested in above comment , we can use following approach in this kind of problems ( binding element to dynamic elements ) -

$("#container").on("focusin", function(){

    if($(this).hasClass("fancybox-bind")){                //check if custom class exist 
       return 0;                                          //now fancybox event will not be binded
    }else{                                                //add class to container
        $(this).addClass("fancybox-bind");
    }

    $("a.ajaxFancyBox").fancybox({                        // fancybox API options here
        'padding': 0
    }); // fancybox

   $("a.iframeFancyBox").fancybox({                       // fancybox API options here
     'padding': 0,
     'width': 640,
     'height': 320,
     'type': 'iframe'
   }); // fancybox
}); // on
想挽留 2025-01-05 23:22:48

I answered the question same like this you can find the answer at

Appending dynamically generated html using jQuery does not play well with Fancybox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文