javascript - 如何将我的 getAttribute 属性添加到我的 localStorage
问题描述
我正在尝试创建一个购物车,以下是我需要遵循的步骤: 第 1 步:找到一种方法,通过单击按钮将商品信息(例如价格和描述)从商品页面移动到购买页面。enter code here
第二步:想办法从商品信息中提取价格并将它们加在一起。第 3 步:想办法将价格加在一起然后从中扣除税款?
所以对于第 1 步:我正在使用 getAttribute 属性来识别我想要移至购买页面的信息,我认为 getAttribute 属性是我可以在从项目移出后提取购买页面上所有价格的最佳方式页面,因此我需要找到一种方法来本地存储 getAttribute 属性。
到目前为止,我已经创建了测试 HTML 和 js 代码来尝试将信息从一个 HTML 页面移动到另一个页面。
function ready() {
var First = document.getElementById('test');
First.getAttribute("data-price");
First.getAttribute("data-name");
First.getAttribute("data-description");
sessionStorage.data-price;
};
alert(sessionStorage.setItem("data-price",First))
function show(){
sessionStorage.getItem(sessionStorage.setItem("data-price",First))
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
show();
</script>
<script type="text/javascript" src="store.js"></script>
</body>
</html>
解决方案
使用数据集获取data-
属性值。
function ready() {
var First = document.getElementById("test");
const price = First.dataset.price;
const name = First.dataset.name;
const description = First.dataset.description;
sessionStorage.setItem("price", price);
sessionStorage.setItem("name", name);
sessionStorage.setItem("description", description);
}
function show() {
const price = sessionStorage.getItem('price');
alert(price);
}
推荐阅读
- reactjs - 键入'FlattenInterpolation
>' 不可分配给类型 'FlattenSimpleInterpolation' - java - 从命令行 2021 使用 maven 打包 apk
- android - Android 设备无法连接到 docker 上的本地 Web 服务器
- c++ - C++ - 我如何频率计数字符?
- python - 如何编写枚举循环
- visual-studio - 在 Visual Studio 中禁用直接查询模式
- casting - Chromecast 网络接收器 + DASH,更喜欢 6ch 音轨
- python - 我怎样才能进入日期时间周年?
- azure - 应用程序洞察力 kusto 查询列出所有子项
- python - 字符串对象的 removeprefix 不是属性