本地存储和DOM:在我使用设置员访问页面之前,内容是不可见的 [英] Localstorage and DOM: Content is invisible until i visit the page with the setters

查看:93
本文介绍了本地存储和DOM:在我使用设置员访问页面之前,内容是不可见的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是网站,如果向下滚动至底部,您会发现所有内容都不可见:这可能需要DOM与LocalStorage冲突.

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.

当您与二传手一起访问页面时,回到首页页面,您会发现内容又回来了.

When you visit the page with the setters, and go back to the home page you will notice the content is coming back.

这是我的代码:

带有设置员的页面(父文件):

//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包含在所有其他html文件中:

custom.js included in every other html file:

 //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);

推荐答案

您尚未在索引页面中设置存储项,而是在其他页面中设置了存储项.在索引页面中,您仅使用了custom.js,它似乎是在获取项目而不是在进行设置.因此,您正在执行的流程是,从索引页面(不存在)中的存储中获取项目,然后仅从其他页面中将项目设置为存储中(当然,用户可以看到他第二次访问索引因此).您需要先设置项目.

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. 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.

这篇关于本地存储和DOM:在我使用设置员访问页面之前,内容是不可见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆