javascript $(window).hashchange() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10597007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:24:21  来源:igfitidea点击:

$(window).hashchange() doesn't work

javascriptjqueryajax

提问by Thomas Saldi

Hi,

I'm trying to use the browser back button, i understood how to catch the event with hashchange plugin =>

我正在尝试使用浏览器后退按钮,我了解如何使用 hashchange 插件捕获事件 =>

$(window).hashchange( function(){
    alert( location.hash );
});$(window).hashchange();

When i try to load the new page, nothing happens.. Is there a way to "reload" the page with the new url ?

当我尝试加载新页面时,没有任何反应.. 有没有办法用新的 url “重新加载”页面?

Thanks !

谢谢 !

回答by Robin Castlin

Try this instead:

试试这个:

$(window).on('hashchange', function(){
    // Your code goes here
}).trigger('hashchange'); // bind event to the same selector as event-listener

.trigger()basicly activates events manually.

.trigger()基本上手动激活事件。

EDIT:
This should be enough for you to work.

编辑:
这应该足以让您工作。

Try this piece of codeand see if you got any luck.

试试这段代码,看看你有没有运气。

Included javascript.js is compressed with jquery and hashchange.

包含的 javascript.js 是用 jquery 和 hashchange 压缩的。

回答by Jasper Giscombe

Put $(window).bind('hashchange', function() {});outside of the document.ready

放在$(window).bind('hashchange', function() {});document.ready之外