ajax - jQuery hashchange 怎么办?

标签 ajax jquery

我做了一个 jQuery 的东西; with 将加载内容而不刷新页面。其代码是:

$(document).ready(function(){
// initial
$('#content').load('content/index.php');

// handle menu clicks
$('#navBar ul li ').click(function(){
var page = $(this).children('a').attr('href');
$('#content').load('content/'+ page +'.php');
return false;
});
});

现在我想要有一些历史,代码是:

(function(){
// Bind an event to window.onhashchange that, when the hash changes, gets the
// hash and adds the class "selected" to any matching nav link.
$(window).hashchange( function(){
var hash = location.hash;
// Set the page title based on the hash.
document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
// Iterate over all nav links, setting the "selected" class as-appropriate.
$('#nav a').each(function(){
var that = $(this);
that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'selected' );
});
})
// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).hashchange();
});

发现于:http://benalman.com/code/projects/jquery-hashchange/examples/hashchange/

我的问题是:如何使第二个代码与第一个代码一起使用?

最佳答案

既然你还没有得到答案,我就写一下。您需要插件 jQuery hashchange 才能运行代码。

https://github.com/cowboy/jquery-hashchange

关于ajax - jQuery hashchange 怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9786383/

相关文章:

javascript - 调整 iFrame 内的 HTML - Javascript 不工作

php - jQuery、javascript 数组/对象 SQL 注入(inject)问题

javascript - 如何基本上像 jQuery masonry 那样重新安排 DIV?

javascript - 从字符串中获取两个数字

javascript - 为什么我的 jQuery 函数在 IE 8 中导致 'Stack Overflow' 错误?

javascript - 使用 JSON 相对于 XML 的差异/优点,反之亦然?

ajax - 通过 List<HttpPostedFileBase> 发布多个文件

iOS 9 WKWebView 产生 "failed to load resource: cancelled"而 iOS 8 没有

javascript - 2人回合制游戏逻辑

jQuery - 将内容加载到 div 中,未应用样式?