简体   繁体   English

SessionStorage导航

[英]SessionStorage Navigation

Greets, so I I'm trying to learn jquery/javascript storage and ran into an issue where I am building a navigation that should remember and place you where you last were if you refresh the page (that's why i use sessionStorage rather then localStorage). 问候,所以我试图学习jquery / javascript存储,并遇到一个问题,即我在构建导航时应该记住并把你放在最后一个刷新页面的位置(这就是为什么我使用sessionStorage而不是localStorage的原因) 。

But I can't seem to change the sessionStorage variable to the new location, nor figure out a functional way to get the user back on refreshing the page. 但是我似乎无法将sessionStorage变量更改为新位置,也无法找出一种使用户重新刷新页面的功能方法。

This is what I currently have. 这就是我目前所拥有的。

$(function() {

sessionStorage.setItem('position', '.first');
var location = sessionStorage.getItem('position');

$('li', this).click(function() {
    cls = $(this).text();

    if('.' + cls != location) {

        $(location).slideToggle(400);
        $('.' + cls).delay(400).slideToggle(400);

            sessionStorage.setItem('position', '.' + cls)
            console.log(location)
    };
  });
});

https://jsfiddle.net/Unkn0wn96/ndj9sqpe/ https://jsfiddle.net/Unkn0wn96/ndj9sqpe/

The code works in an very odd way, rather then how it's intended, including never changing the value when I console.log(location). 该代码以一种非常奇怪的方式工作,而不是它的预期工作方式,包括当我console.log(location)时从不更改值。

I made some further testing on this and found a 'more' working way in the was that the sessionStorage does change to something, yet it not being usefull. 我对此进行了进一步测试,发现其中的一种“更多”工作方式是sessionStorage确实更改为某种东西,但没有用。

https://jsfiddle.net/Unkn0wn96/nkbtykkr/ https://jsfiddle.net/Unkn0wn96/nkbtykkr/

but yet, they elements don't toggle as they should, including that when I console log sessionStorage.position it returns NaN. 但是,它们的元素并没有按应有的方式切换,包括当我控制台日志sessionStorage.position时返回NaN。 Also for some odd reason I can't store sessionStorage.position within a variable, it just refuses to change its value. 同样由于某种奇怪的原因,我无法将sessionStorage.position存储在变量中,它只是拒绝更改其值。 I have no clue why this is happening. 我不知道为什么会这样。

So I was finally able to solve my issue with sessionStorage, since there was not much of help I'd assume the information regarding the case was limited and so wish to share my solution. 因此,我最终可以通过sessionStorage解决我的问题,因为我没有太多帮助,我认为与案件有关的信息有限,因此希望分享我的解决方案。 All thought it does not work fully as intended it does execute it's main function. 所有人都认为它不能按预期的方式正常运行,而是执行了它的主要功能。

$(function() {

  $('button').on('click', function() {
  var msg = '.'+$(this).text();

    localStorage.setItem('color', 'blue') //Default (does not matter since the if statement does not register)

  if(msg != '.' + localStorage.color) { // DOES NOT REGISTER

    if(localStorage.length > 0) {
       localStorage.removeItem('color')
       console.log('localStorage.length : ' + localStorage.length)
    };

            localStorage.setItem('color', msg)
      console.log(localStorage.color)
      } else {
      console.log('cancelled')
      }
});
});

https://jsfiddle.net/mdqjoz69/4/ https://jsfiddle.net/mdqjoz69/4/

So what I finally was able to achieve was to make a localStorage key change it's value regarding of what button you press. 因此,我最终能够实现的是对localStorage键进行更改,使其与您按下的按钮有关。

What I failed to achieve was being able to check the stored data to not execute whenever you store something that have already been stored. 我无法实现的是,无论何时存储已存储的内容,都无法检查存储的数据是否执行。 There might be a work around for this. 为此可能需要解决。 But since the code serves its main purpose I'll let it slide. 但是由于代码具有主要目的,因此我会让它滑动。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM