jQuery绑定popstate事件未通过

时间:2022-11-07 12:06:43

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...

当我点击“上一步”按钮时,它会记录'pop:undefined'...

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有问题吗?或者我弄乱了什么?

1 个解决方案

#1


34  

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: *.com/questions/7860960/popstate-returns-event-state-is-undefined

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

event.originalEvent.state

#1


34  

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: *.com/questions/7860960/popstate-returns-event-state-is-undefined

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

event.originalEvent.state