本文介绍了popstate返回event.state未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本示例中了解HTML5中的历史记录(打开JavaScript浏览器控制台以查看错误)事件是jQuery事件对象,而不是DOM。



要访问DOM事件对象,请使用 event.originalEvent :。

  var state = event.originalEvent.state; 

请记住,状态仅在新状态包含数据时定义,因此在单击时不可用然后回到初始状态:


  1. 初始状态

  2. 链接到状态1

  3. 返回按钮至初始状态(无可用数据)

然而,点击,点击另一个时间然后返回:


  1. 初始状态

  2. 链接到状态1

  3. 链接至状态2

  4. 后退按钮至状态1(可用数据)


I am learning about history in HTML5, in this example (open the JavaScript browser console to see error) the event.state.url returns:

Uncaught TypeError: Cannot read property 'url' of undefined

Look and help: http://jsfiddle.net/un4Xk/

解决方案

event is the jQuery event object, not the DOM one.

To access the DOM event object, use event.originalEvent: http://jsfiddle.net/pimvdb/un4Xk/1/.

var state = event.originalEvent.state;

Remember that the state is only defined when the new state has data, so it is not available when clicking and then going back to the initial state:

  1. initial state
  2. link to state 1
  3. back button to initial state (no data available)

It is, however, available when clicking, clicking another time and then going back:

  1. initial state
  2. link to state 1
  3. link to state 2
  4. back button to state 1 (data available)

这篇关于popstate返回event.state未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:08