jQuery bind popstate event not passed

笑着哭i 提交于 2019-11-29 16:09:10

问题


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

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

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 ?


回答1:


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


来源:https://stackoverflow.com/questions/10299980/jquery-bind-popstate-event-not-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!