如何将 fancybox 绑定到动态添加的元素?
我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 fancybox (v1.3.x) 绑定到动态添加的元素的最简单方法是:
1: 升级到 jQuery v1.7.x(如果还没有)
2: 使用 jQuery API
on()
+focusin
事件。为了使其工作,您需要根据上面的代码使用
class="ajaxFancyBox"
找到元素的parent
元素(或parent
的parent
,因为你需要它)并将jQuery on()
附加到它,例如,有这个 html:.. 我们将应用
on()< /code> 和
focusin
事件到元素id="container"
(parent
)如上例所示,例如:您可以根据需要应用任何 fancybox 选项。此外,您可能对不同类型的内容(在
on()
方法内)使用不同的脚本,例如:重要:上面的示例在 Chrome 上的工作方式不同。 解决方法是将
tabindex
属性添加到绑定到 fancybox 的所有元素中,这样可以解决问题,并且可以在包括 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()
+ thefocusin
event.To make it work you need to find the
parent
element of your elements withclass="ajaxFancyBox"
as per your code above (or theparent
of theparent
as you need it) and attachjQuery on()
to it so for example, having this html:.. we will apply
on()
andfocusin
event to the element withid="container"
(theparent
) as in the example above, like: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: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 likethat 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.您指定了错误的类名,请尝试以下操作:
You're specifying the wrong class name, try this instead:
你可以尝试这样的事情:
然后创建一个函数来创建并显示 Fancybox:
You could try something like this maybe:
And then make a function to create and show Fancybox:
正如上面评论中所建议的,我们可以在此类问题中使用以下方法(将元素绑定到动态元素) -
As suggested in above comment , we can use following approach in this kind of problems ( binding element to dynamic elements ) -
我回答了同样的问题,你可以在
使用 jQuery 附加动态生成的 html 与 Fancybox 配合不佳
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