我正在使用pjax来控制页面&URL,所以部分内容&当我按下导航按钮时,URL将在不刷新的情况下更改,
但现在我面临的问题是,我在页面中插入了一个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>
在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));
});
});
});
},