2016-08-12 155 views
1

我需要在网站加载时使用hashtag更改网站页面。 例如,当在浏览器地址栏中输入:检查页面加载时的网址

http://example.com/#about

有关在浏览器中打开页面。 我用这个:

$(window).bind('hashchange', function(e){ 
    var anchor = document.location.hash; 
    nextPage(anchor); 
}); 

变更页上时,菜单按钮的用户请它工作得很好,但我需要在第一次加载网站的这一检查发生太大。

回答

1

如果你想立即触发事件,请使用trigger()

$('selector').on('event', function(e) { ... }).trigger('event'); 

所以,你会怎么做:

$(window).bind('hashchange', function(e){ 
    var anchor = document.location.hash; 
    nextPage(anchor); 
}).trigger('hashchange');