简体   繁体   中英

Facebox doesn't work when content is added via jQuery?

I have a website that loads content via jQuery. When loading all my javascript and css files needed for facebox. It I put a link on the initial page, it works fine. However, when changing the 'innerHTML' of one element, the same links do not function.

And I even tried putting the javascript & css files in the innerHTML and it still doesn't work! It loads up as a new page.

Please help! Thanks

Edit 1:

index.php

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="http://justtwobros.com/actions.js"></script>
<link href="src/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="lib/jquery.js" type="text/javascript"></script>
<script src="src/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
 jQuery(document).ready(function($) {
   $('a[rel*=facebox]').facebox({
     loadingImage : 'src/loading.gif',
     closeImage   : 'src/closelabel.png'
   })
 })
</script>

<a href="file.html" rel="facebox">text</a> (this link is the one that works)

<script>
    showAllWorkOnLoad();
</script>

actions.js

function showAllWorkOnLoad() {
var thePostData = "filter=allwork";
$.ajax({
    type: "POST",
    url: "http://justtwobros.com/get_work.php",
    data: thePostData,
    success: function(theRetrievedData) {
        document.getElementById('content').innerHTML = theRetrievedData;
        $("#content").fadeIn(20);
        focusButtonAllWork();

        var count = 0;
        $("#content").find('.work_thumbnail_wrapper').each(function() {
            count++;
            var timeWait = (count * 100) + 500;
            var theId = $(this).attr('id');  
            var theIdDone = "#" + theId;
            setTimeout(function(){$(theIdDone).fadeIn(300);},timeWait);
        }); 

    }
});
}

get_work.php

<a href="file.html" rel="facebox">text</a> (this link that doesn't work)

It doesn't work because that link does not have a listener attached to it. You can confirm this is your problem by adding this code after your ajax call (on success, after you have inserted the link into the document).

   $('a[rel*=facebox]').facebox({
     loadingImage : 'src/loading.gif',
     closeImage   : 'src/closelabel.png'
   })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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