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?
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()+ thefocusinevent.To make it work you need to find the
parentelement of your elements withclass="ajaxFancyBox"as per your code above (or theparentof theparentas you need it) and attachjQuery on()to it so for example, having this html:.. we will apply
on()andfocusinevent 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
tabindexattribute 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
tabindexworkaround for Chrome also applies.