简体   繁体   English

通过jQuery添加内容时Facebox无法正常工作吗?

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

I have a website that loads content via jQuery. 我有一个通过jQuery加载内容的网站。 When loading all my javascript and css files needed for facebox. 当加载我的facebox所需的所有javascript和css文件时。 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. 但是,当更改一个元素的“ innerHTML”时,相同的链接不起作用。

And I even tried putting the javascript & css files in the innerHTML and it still doesn't work! 我什至尝试将javascript和css文件放入innerHTML中,但仍然无法正常工作! It loads up as a new page. 它将作为新页面加载。

Please help! 请帮忙! Thanks 谢谢

Edit 1: 编辑1:

index.php 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 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 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). 您可以通过在ajax调用之后添加此代码来确认这是您的问题(成功后,将链接插入文档中)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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