[英]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.