Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
How to display local storage to HTML page?

I have a project. It is about to people search something in search page and when they come back to home page, searching history will show on home page. I have pushed searching data to local storage, with key and value. The problem is I dont know how to show it to my html page. Have you worked with the same thing like me? Thanks a lot. Needs some comment to solve my problem.

<section class="container mb-5">
<div class="row">
        <h2 id="div_title" class="ttl-art01">検索履歴<span class="title"></span></h2>
</div>
<ul class="accordion-area">
    <li>
        <section id="list_box">
            <div class="box-active">
                
            </div>
            <div class="box">
                
            </div>
            <div class="box">
                
            </div>
            <div class="box">
                
            </div>
            <div class="box">
                
            </div>
        </section>
    </li>
</ul>

And this is my script

<script type="text/javascript">
function getData() {
    return this.$storage.get('listKey');
}
window.onload = function () {
    const div = document.querySelectorAll("#list_box > div");
    const tdiv = div.parentNode;
    const data = getData();
    for (let i = 1; i< data.length; i++) {
        tdiv.appendChild(div.cloneNode(true));
    }
    data.forEach((row, i) => Object.keys(row).forEach(prop => 
        tdiv.rows[i+1].querySelector('.' + prop).textContent = row[prop]);
    );
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

document.querySelectorAll returns a NodeList, that's what you got wrong.

about 4 years ago · Juan Pablo Isaza Relatório

0

In your code, you're referencing local storage as this.$storage, I'm not sure what wrapper this is or from what library, but I'm going to assume you want to interact with HTML5 localStorage.

localStorage stores strings so if you want to store a more complex data structure, you need to transform it.

You can transform an array or an object using the JSON.stringify method.

To get the content of local storage at a later time and if IS NOT a plain string, you would use the JSON.parse method.

In the code below you can see that getFromLocalStorage and saveToLocalStorage use the JSON.parse and JSON.stringify methods respectively.

  <script type="text/javascript">
      function getFromLocalStorage(localStorageKey = "items") {
        return JSON.parse(localStorage.getItem(localStorageKey)) || [];
      }

      function saveToLocalStorage(localStorageKey = "items", value) {
        const items = getFromLocalStorage();
        const newItems = [...items, value];

        localStorage.setItem("items", JSON.stringify(newItems));
      }

      window.onload = function () {
        const items = getFromLocalStorage();

        items.forEach((item) => {
         // do something with items.
        });
      };
    </script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda