我有一个功能WordPress主题,通过ajax加载内容。我遇到的一个问题是,当页面直接加载时,ajax脚本不再起作用。例如,链接结构的工作原理如下,在www.example.com上点击关于页面链接,链接将变成www.example.com/#/about。但是,当我直接加载独立页面www.example.com/about时,从此页面单击的其他链接变为www.example.com/about/#/otherlinks。我从这个tutuorial http://www.deluxeblogtips.com/2010/05/how-to-ajaxify-wordpress-theme.html修改了一些代码。这是我的代码。谢谢您的帮助。ajax加载URL hashchange问题
jQuery(document).ready(function($) {
var $mainContent = $("#container"),
siteUrl = "http://" + top.location.host.toString(),
url = '';
$(document).delegate("a[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/]))", "click", function() {
location.hash = this.pathname;
return false;
});
$(window).bind('hashchange', function(){
url = window.location.hash.substring(1);
if (!url) {
return;
}
url = url + " #ajaxContent";
$mainContent.fadeOut(function() {
$mainContent.load(url,function(){
$mainContent.fadeIn();
});
});
});
$(window).trigger('hashchange');
});
感谢您的回应,这些链接非常棒,我会告诉你它是如何结束的。 –
如果这篇文章回答了您的问题,您可能需要通过单击复选标记将其标记为正确。谢谢〜 – hazerd
我结束了与history.js去摆脱所有的hashchanges –