通过 History.pushState() 函数传递 jQuery 对象
我正在使用 History.pushState() 函数,我将 jQuery 对象保存到推送状态对象中,但是当触发 statechange 事件时,我正在检索没有 jQuery 对象的状态对象数据,但只有“正常”对象:
var object = {link : $("#varianty")}; console.log(object.link); History.pushState(object,"test","test.php");
控制台返回 jQuery对象
<代码> jQuery(div#varianty.box)
但是当 statechange 事件被触发时:
History.Adapter.bind(window,'statechange',function(){ var State = History.getState(); var link = State.data.link; console.log(link); });
控制台返回一个对象
对象 { 长度=1, 0={...}, 更多...} 是否
详细转储
0 => Object { jQuery1304643255828=39, constructor={...}} context => Object { location={...}} length => 1 selector => "#varianty"
有机会在状态对象 jQuery 对象中检索,如果没有,没有很酷的方法,但我可以通过以下方式再次选择对象: $(State.data.link.selector);
但是通过pushState对象传递jQuery对象是没有用的...
需要它在点击时推送$(this)
对象事件。
感谢您的任何建议或解决方案! :)
I'm using History.pushState() function, I'm saving into push state object an jQuery object, but when statechange event is fired I'm retriving state object data without jQuery object, but only "normal" object:
var object = {link : $("#varianty")}; console.log(object.link); History.pushState(object,"test","test.php");
Console returns jQuery object
jQuery(div#varianty.box)
But when the statechange event is fired:
History.Adapter.bind(window,'statechange',function(){ var State = History.getState(); var link = State.data.link; console.log(link); });
Console return an object
Object { length=1, 0={...}, more...}
Detailed dump
0 => Object { jQuery1304643255828=39, constructor={...}} context => Object { location={...}} length => 1 selector => "#varianty"
Is there any chance to retrive in state object jQuery object, if no, there is not cool way but I can again select the object by:$(State.data.link.selector);
But then is useless to pass jQuery object through pushState object...
Need it to push $(this)
object on click event.
Thanks for any advice or solution! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
传递给 pushState 函数的对象在 popstate 事件中生成时似乎会丢失其原型。为了解决这个问题,您可以在侦听事件时重新附加原型。
在你的情况下,最好只将你需要的元素的选择器传递到pushState中,
这样你就可以使用jQuery从popstate事件的dom中获取元素
Objects that you pass to the pushState function seem to loose their prototype when yielded in the popstate event. To get around this you can re-attach the prototype when listening to the event.
In your case though it might be better to pass just the selector for the element you need into pushState
That way you can just grab the element from the dom on the popstate event with jQuery