简体   繁体   English

本地存储和DOM:在我使用设置员访问页面之前,内容是不可见的

[英]Localstorage and DOM: Content is invisible until i visit the page with the setters

This is the website , if you scroll down at the bottom you will notice everything is invisible: This probably has to do with DOM conflict with LocalStorage. 这是网站 ,如果您向下滚动底部,您会发现所有内容都不可见:这可能与与LocalStorage的DOM冲突有关。

在此处输入图片说明

When you visit the page with the setters, and go back to the home page you will notice the content is coming back. 当您访问带有设置员的页面并返回首页时,您会发现内容又回来了。

Here is my code: 这是我的代码:

Page with the setters (Parent file): 带有二传手的页面 (父文件):

//code is at the end of the body of the page

    localStorage.setItem("tit1",$('#r1c1Title').text());
    localStorage.setItem("tit2",$('#r1c2Title').text());

   localStorage.setItem("l1",document.getElementById("r1c1Link").href);
   localStorage.setItem("l2",document.getElementById("r1c2Link").href);

  localStorage.setItem("im1",$('#r1c1Image').attr('src'));
   localStorage.setItem("im2",$('#r1c2Image').attr('src'));

custom.js included in every other html file: 每个其他html文件中包含的custom.js

 //Retrieve Titles
    var result  = localStorage.getItem("tit1");
    var result2 = localStorage.getItem("tit2");

     //Retrieve Links
     var r3= localStorage.getItem("l1");
     var r33= localStorage.getItem("l2");

     //Retrieve Image SRCs
     var r4 = localStorage.getItem("im1");
     var r44 = localStorage.getItem("im2");



    //print titles
    $("#ti1").html(result);
    $("#ti2").html(result2);

   //print links
    $("#link1").attr("href", r3);
    $("#link2").attr("href", r33);

   //print images
       $("#img1").attr("src",r4);
       $("#img2").attr("src",r44);

You have not set the items in storage in your index page but in the other pages. 您尚未在索引页面中设置存储项,而是在其他页面中设置了项。 In the index page, you have just used custom.js that seems to be getting items rather than setting. 在索引页面中,您仅使用了custom.js ,它似乎在获取项目而不是设置。 So flow you are doing is, getting the item from the storage from index page(which does not exist) and then only setting item to the storage from other page(and of course, the user can sees the second time he goes to the index page because of this). 因此,您正在执行的流程是,从索引页面(不存在)中的存储中获取项目,然后仅从其他页面中将项目设置为存储中(当然,用户可以看到他第二次访问索引因此)。 You need to set the item first. 您需要先设置项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在我的网站中使用 localStorage 进行翻译,而不是每次我再次访问该页面时加载它们 - Using localStorage for translations in my website instead of loading them every time i visit the page again 如何对由 DOM 上的 setter 组成的函数进行单元测试? - How can I unit test a function comprised of setters on the DOM? 单击一次会不必要地进行两次 ajax 调用,并且在我刷新页面之前 DOM 不会更新 - single click makes two ajax call unwantedly and DOM not updated until i refresh my page firefox插件中面板内容脚本和页面内容脚本之间的localStorage - localStorage between panel content script and page content script in firefox addon 如何在localStorage到期之前停止倒计时? - How can I stop the countdown until the localStorage expires? 显示page1直到page2 dom完全创建 - display page1 until page2 dom is completly created 如何将 localStorage 中的数据附加到 DOM 上的元素 - How do I append data from localStorage to an element on the DOM 如何在不刷新页面的情况下将内容附加到html页面并刷新DOM? - How can i append content to my html page and refresh the DOM without refresh the page? 如何将此页面保存到 localStorage - How can I save this page to localStorage 在刷新页面之前,反应内容数据不正确 - React content data not correct until page refreshed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM