为什么不能对jQuery对象进行字符串化? [英] Why can't you stringify a jQuery object?

查看:98
本文介绍了为什么不能对jQuery对象进行字符串化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON.stringify($(p)); 会导致错误:

InvalidStateError: Failed to read the 'selectionDirection' property from 'HTMLInputElement': The input element's type ('button') does not support selection.

(我正在使用Google Chrome 34)

为什么?

我还应该怎样做 $(p)更便携,以便我可以存储或传递它消息?

How else should I make $("p") more portable so I can store it or pass it in a message?

推荐答案

有大量的状态(属性,事件处理程序,与这些相关的代码,内部状态,... 。)涉及HTML元素。将所有这些序列化为JSON是没有意义的。

There's a ton of state (attributes, event handlers, the code related to those, internal state, ...) involved in an HTML element. It just doesn't make sense to serialize all of that into JSON.

如果你想获得元素的某种表示 JSON,您可以使用 .html()来获取表示该元素的HTML字符串。或者提出一种格式,例如,仅对标签名称,属性和文本进行编码。您可能必须手动实现(或找到一个库 - html to json可能是一个很好的关键字)

If you want to get some kind of representation of the element in JSON, you could for instance use .html() to get a HTML string representing the element. Or come up with a format that encodes, for instance, tag names, attributes and text only. You could have to implement that by hand though (or find a library - "html to json" could be a good keyword)

这篇关于为什么不能对jQuery对象进行字符串化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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