gpt4 book ai didi

javascript - 如何从 sessionStorage 中存储的 JSON 值中检索特定对象?

转载 作者:行者123 更新时间:2023-12-03 00:35:20 25 4
gpt4 key购买 nike

我将其存储在 session 中:

sessionStorage screen grab

我想要做的是将 JSON 中的每个对象分配为变量,以便我可以将它们适本地添加到 DOM 中。

这有效,但会打印出所有内容:

if (sessionStorage.getItem('wc_fragments_aaf6a2e5b971cb51d59e8f3eae9b34c9') != null) {
$(sessionStorage.getItem('wc_fragments_aaf6a2e5b971cb51d59e8f3eae9b34c9')).appendTo('.div');
}

我想要的是这样的东西,但它不起作用:

var div1 = $(JSON.parse(sessionStorage.getItem('wc_fragments_aaf6a2e5b971cb51d59e8f3eae9b34c9', 'a.cart-contents')));
var div2 = $(JSON.parse(sessionStorage.getItem('wc_fragments_aaf6a2e5b971cb51d59e8f3eae9b34c9', 'a.footer-cart-contents')));
var div3 = $(JSON.parse(sessionStorage.getItem('wc_fragments_aaf6a2e5b971cb51d59e8f3eae9b34c9', 'div.widget_shopping_cart_content')));

任何帮助将不胜感激。谢谢!

最佳答案

多次从存储获取相同的值并不是一个好主意。此外,您需要为变量提供更好的名称。

var json = sessionStorage.getItem('wc_fragments_aaf6a2e5b971cb51d59e8f3eae9b34c9');
if (json) {
var data = JSON.parse(json);
if (data) {
var cart_link = $(data['a.cart-contents']),
footer_link = $(data['a.footer-cart-contents']),
widget_div = $(data['div.widget_shopping_cart_content']);
}
}

关于javascript - 如何从 sessionStorage 中存储的 JSON 值中检索特定对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com