【问题标题】:localstorage.getItem shows only one data although there is more than one data in the arraylocalstorage.getItem 仅显示一个数据,尽管数组中有多个数据
【发布时间】:2020-10-03 15:00:41
【问题描述】:

虽然数组项数组中存储了多个数据,但它只显示最近添加的数据。这可能是什么原因? 提前感谢您的支持

    // List items
    const array_item = JSON.parse(localStorage.getItem("array_item") || "[]");

    array_item.forEach(function(item,index,array){
        const output = '<div class="bg-card p-2" style="font-size:14px;"><div class="container"><strong>Okunmadı olarak işaretlediğin makale:</strong> <a href="<?=PROOT?>home/details/'+item.sef_link +'">'+item.title+'</a></div></div>';
        console.log(array);
        document.getElementById("mark").innerHTML = output;
    });

【问题讨论】:

  • 您每次都在替换内容而不是追加内容!
  • 我没有做任何改变它。我只是想在数组项数组中提取数据。我在哪里犯错了?
  • document.getElementById("mark").innerHTML = output 每次都替换 HTML,而不是追加到它上面!

标签: javascript arrays json local-storage


【解决方案1】:
const array_item = JSON.parse(localStorage.getItem("array_item") || "[]");

    array_item.forEach(function(item,index,array){
        const output = '<div class="bg-card p-2" style="font-size:14px;"><div class="container"><strong>Okunmadı olarak işaretlediğin makale:</strong> <a href="<?=PROOT?>home/details/'+item.sef_link +'">'+item.title+'</a></div></div>';
        console.log(array);
        document.getElementById("mark").innerHTML += output;
    });

【讨论】:

  • 添加新数据时,刷新页面前看不到。为了这;那么我应该怎么做才能立即显示呢?你知道这个话题吗?
猜你喜欢
  • 2016-04-05
  • 2014-09-17
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多