未通过jQuery绑定popstate事件

编程入门 行业动态 更新时间:2024-10-17 07:30:02
本文介绍了未通过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事件对象来实现。你可以在这里找到一些额外的细节和例子: stackoverflow/questions/7860960/popstate- return-event-state-is-undefined

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

event.originalEvent.state

更多推荐

未通过jQuery绑定popstate事件

本文发布于:2023-11-27 21:59:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   事件   jQuery   popstate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!