首页 > 解决方案 > 如何将我的 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>

标签: javascriptsession-storagegetattribute

解决方案


使用数据集获取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);
 }

推荐阅读