为什么不能对jQuery对象进行字符串化呢?

时间:2022-12-27 21:13:26

The line JSON.stringify( $("p") ); causes an error:

JSON。stringify($(p));会导致一个错误:

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

(I'm using Google Chrome 34)

(我用的是谷歌Chrome 34)

Why?

为什么?

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

我还应该如何使$(“p”)更便携,以便我可以存储或传递到消息中?

1 个解决方案

#1


8  

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.

HTML元素中包含大量的状态(属性、事件处理程序、与这些相关的代码、内部状态等等)。将所有这些序列化为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)

如果希望在JSON中获得某种元素的表示,可以使用. HTML()获取表示该元素的HTML字符串。或者设计一种只对标签名、属性和文本进行编码的格式。您可以手工实现它(或者找到一个库——“html到json”可能是一个很好的关键字)

#1


8  

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.

HTML元素中包含大量的状态(属性、事件处理程序、与这些相关的代码、内部状态等等)。将所有这些序列化为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)

如果希望在JSON中获得某种元素的表示,可以使用. HTML()获取表示该元素的HTML字符串。或者设计一种只对标签名、属性和文本进行编码的格式。您可以手工实现它(或者找到一个库——“html到json”可能是一个很好的关键字)