0

我是用pjax来控制页面&网址,所以当我按下导航按钮时,部分内容&网址会被改变而不刷新,

但现在我面临一个问题,即我在页面内插入了一个 facebook 评论插件,当第一次显示 facebook 评论插件时它只是可行的,但是一旦我将页面从另一个页面导航到 facebook 评论插件页面,它不再工作了,我试图删除 '//connect.facebook.net/en_US/all.js#xfbml=1&appId=xxxxxxxx' 并在每次浏览此页面时重新加载它,但结果仍然是同样,谁能帮我解决这个问题?非常感谢。

我的代码如下: -

//index.php的一部分

<div id="wrapper">
    <div id="content">
        <div id="navigation">
            <div id="timer">1</div>
            <ul>
                <li>
                    <a href="/html5/sample3/finished/index.php" title="You Get What You Give">
                        You Get What You Give
                    </a>
                    <a href="/html5/sample3/finished/index.php?page=fortune-faded" title="Fortune Faded">
                        Fortune Faded
                    </a>
                    <a href="/html5/sample3/finished/index.php?page=liar" title="Liar">
                        Liar
                    </a>
                    <a href="/html5/sample3/finished/index.php?page=universal" title="Universal">
                        Universal
                    </a>
                </li>
            </ul>
        </div>

        <div id="container">
            <div id="left">
                <?php echo $content['left']; ?>
            </div>

            <div id="right">            
                <?php echo $content['right']; ?>
            </div>
        </div>

    </div>

    <div class="clearer"></div>
</div>

<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/core.js"></script>




//part of core.js
var systemObject = {
    run : function() {
        this.content($('#navigation ul li a'));
        this.pop();
    },
    content : function(obj) {
        obj.live('click', function(e) {
            var thisUrl = $(this).attr('href');
            var thisTitle = $(this).attr('title');
            systemObject.load(thisUrl);
            window.history.pushState(null, thisTitle, thisUrl);
            e.preventDefault();
        });
    },
    pop : function() {
        window.onpopstate = function() {
            systemObject.load(location.pathname);
        };
    },
    load : function(url) {
        $("#facebook-jssdk").remove();                                  //delete //connect.facebook.net/en_US/all.js#xfbml=1&appId=xxxxxxxxx
        url = url === '/' ? '/ygwyg' : url;
        jQuery.getJSON(url, { ajax : 1 }, function(data) {              //load content
            jQuery.each(data, function(k, v) {
                $('#' + k + ' section').fadeOut(200, function() {
                    $(this).replaceWith($(v).hide().fadeIn(200));
                });
            });
        });
    },
};
$(function() {
    systemObject.run();
});



 //part of index.php?page=fortune-faded
<section>
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=xxxxxxxxx";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<div class="fb-comments" data-href="http://example.com" data-num-posts="2" data-width="470"></div>
</section>
4

1 回答 1

1

在 AJAX 成功函数中显式调用“FB.XFBML.parse()”,这将重新解析 html 并呈现 Facebook 评论部分。

   //facebook comments
    var isFacebook = $data.find('.fb-comments');
    if(isFacebook != 'undefined' ) {
        var scriptText = 'FB.XFBML.parse();';
        var scriptNode = document.createElement('script');
        scriptNode.appendChild(document.createTextNode(scriptText));
        contentNode.appendChild(scriptNode);                   
    } 

对于您的代码,请在内部写入:

   load : function(url) {
        url = url === '/' ? '/ygwyg' : url;
        jQuery.getJSON(url, { ajax : 1 }, function(data) {   //load content
            jQuery.each(data, function(k, v) {

                //here check if the response data contains .fb-contents class
                //Then call the FB.XFBML.parse() function    

                $('#' + k + ' section').fadeOut(200, function() {
                    $(this).replaceWith($(v).hide().fadeIn(200));
                });
            });
        });
    },
于 2012-07-30T18:29:04.757 回答