Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome中JSON的循环结构_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript Chrome中JSON的循环结构

Javascript Chrome中JSON的循环结构,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,为什么下面的代码在Chrome控制台中导致“类型错误:将循环结构转换为JSON” JSON.stringify(document.querySelectorAll("img")) DOM元素包含循环引用,不能对任何包含循环引用或包含循环引用的对象调用JSON.stringify() 例如,它包含.parentNode,然后包含.childNodes,然后指向同一元素。只要JSON.stringify()检测到这样的循环引用,它就会停止,因为它无法记录所有属性 基本上,您不能对DOM节点或DOM

为什么下面的代码在Chrome控制台中导致“类型错误:将循环结构转换为JSON”

JSON.stringify(document.querySelectorAll("img"))

DOM元素包含循环引用,不能对任何包含循环引用或包含循环引用的对象调用
JSON.stringify()

例如,它包含
.parentNode
,然后包含
.childNodes
,然后指向同一元素。只要
JSON.stringify()
检测到这样的循环引用,它就会停止,因为它无法记录所有属性


基本上,您不能对DOM节点或DOM节点列表进行字符串化。也许,如果你能帮助我们理解你真正想解决的问题,我们可以用另一种方法来帮助你解决。例如,为了保存这些图像,您可能只需要将其保存在
img.src
属性中。

您能提供一些详细信息吗?…例如,您需要img的id,或者???我自己从来没有完全理解过这个错误。感谢这个好例子(parentNode/childNodes),所以我使用Array.prototype.map创建了另一个只包含高度、宽度和src的数组。我可以把它具体化。