如何字符串化事件对象? [英] How to stringify event object?

查看:125
本文介绍了如何字符串化事件对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON.stringify(eventObject);

给出:

TypeError:将循环结构转换为JSON

dojox.json.ref.toJson(eventObject);

给出:

TypeError:在无法选择的输入元素上访问selectionEnd。

是否有一些库/代码可以用来完成它?

Is there some library/code ready to use to accomplish it ?

推荐答案

你赢了无法使用JSON.stringify序列化事件对象,因为事件对象包含对DOM节点的引用,并且DOM在整个地方都有循环引用(例如子/父关系)。默认情况下JSON无法处理这些问题,所以你在运气方面有点不幸。

You won't be able to serialize an event object with JSON.stringify, because an event object contains references to DOM nodes, and the DOM has circular references all over the place (e.g. child/parent relationships). JSON can't handle these by default, so you're a bit out of luck there.

我建议你看看如果有循环引用,如何将DOM节点序列化为JSON?,其中有一些建议如何序列化DOM节点。此外,以下问题似乎有用的信息:

I'd suggest to look at How to serialize DOM node to JSON even if there are circular references? which has a few suggestions on how to serialize a DOM node. Also, the following questions seem to have useful information:

  • How to save an object with circular references?
  • Stringify (convert to JSON) a JavaScript object with circular reference

能够处理循环引用的JSON库似乎是

JSON libraries able to handle circular references seem to be

  • JSON-js (see cycle.js)
  • dojox.json.ref

或者,您可以删除对DOM节点的所有引用(如果您不需要它们),然后序列化该对象。您不应该这样做毕竟。请参阅@PointedEars评论:)

Alternatively, you could delete all references to DOM nodes if you don't need them, and then serialize the object. You shouldn't do this after all. See @PointedEars comment :)

这篇关于如何字符串化事件对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆