未通过jQuery绑定popstate事件

未通过jQuery绑定popstate事件

本文介绍了未通过jQuery绑定popstate事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一段历史API的演示。我正在努力解决这个问题:

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

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

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

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);
};

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

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

所以就好像jQuery不会将事件对象传递给回调函数。
jQuery有问题吗?或者我弄乱了什么?

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尚未完成所有新的HTML5事件和相关属性的规范化。在此之前,您需要访问原始事件对象。您可以通过originalEvent属性通过jQuery事件对象来实现。你可以在这里找到一些额外的细节和例子:

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

event.originalEvent.state

这篇关于未通过jQuery绑定popstate事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:57