Why can't you stringify a jQuery object?

后端 未结 1 945
耶瑟儿~
耶瑟儿~ 2020-12-18 04:14

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

InvalidStateError: Failed to read the \'selectionDirection\' property from \'HTMLInputElem         


        
相关标签:
1条回答
  • 2020-12-18 04:45

    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.

    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)

    0 讨论(0)
提交回复
热议问题