jQuery jQuery绑定popstate事件未通过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10299980/
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-08-26 09:06:38  来源:igfitidea点击:

jQuery bind popstate event not passed

jqueryhtmlhtml5-history

提问by Jocelyn LECOMTE

I'm coding a little demo for the History API. And I'm struggling with this:

我正在为 History API 编写一个小演示。我正在为此苦苦挣扎:

$(window).bind('popstate',  
    function(event) {
        console.log('pop: ' + event.state);
    });

It logs 'pop: undefined' when I click on the 'Previous' button...

当我单击“上一个”按钮时,它会记录“流行:未定义”...

But if I do this instead, things are working like expected :

但是如果我这样做,事情就会像预期的那样工作:

window.onpopstate = function(event) {
    console.log('pop: ' + event.state);
};

It logs 'pop: [object Object]' this time...

这次它记录了'pop:[object Object]'......

So it's like jQuery doesn't pass the event object to the callback.
Is there a problem with jQuery ? Or did I mess something ?

所以这就像 jQuery 没有将事件对象传递给回调。
jQuery 有问题吗?还是我搞砸了什么?

回答by KSev

In the first instance, you're getting a normalized jQuery event object. In the second instance, you're getting the browser's event object. I assume that jQuery hasn't completed normalizing all the new HTML5 events and related attributes. Until then, you'll need to access the original event object. You can do that through the jQuery event object via the originalEvent property. You can find some additional details and examples here: stackoverflow.com/questions/7860960/popstate-returns-event-state-is-undefined

在第一个实例中,您将获得一个规范化的 jQuery 事件对象。在第二种情况下,您将获得浏览器的事件对象。我假设 jQuery 还没有完成对所有新 HTML5 事件和相关属性的规范化。在此之前,您需要访问原始事件对象。您可以通过 originalEvent 属性通过 jQuery 事件对象执行此操作。您可以在此处找到一些其他详细信息和示例:stackoverflow.com/questions/7860960/popstate-returns-event-state-is-undefined

event.originalEvent.state