简体   繁体   English

如何使用 sessionStorage 在 JavaScript 中保持 css 样式更改

[英]How to keep css style changes in JavaScript with sessionStorage

I am trying to make a button that toggles the CSS display property between block and none.我正在尝试制作一个在块和无之间切换 CSS 显示属性的按钮。 I'm trying to make it so a user can turn a background colour on and off, and then keep the selection when they move between pages.我试图让用户可以打开和关闭背景颜色,然后在页面之间移动时保持选择。 Just to be more awkward, I have to be able to do this with inline-CSS and vanilla JavaScript only.只是为了更尴尬,我必须只能使用内联 CSS 和 vanilla JavaScript 来做到这一点。

So far, I've managed to get the display property to toggle, but I can't work out how to implement sessionStorage.到目前为止,我已经设法让 display 属性切换,但我不知道如何实现 sessionStorage。 I've tried this a million different way.我已经尝试过一百万种不同的方式。 The example below was my latest attempt.下面的例子是我最近的尝试。

Any help would be hugely appreciated :)任何帮助将不胜感激:)

<div onchange="colours()" id='red' width='100%' height='auto' style="background-color:red; padding: 20%; display:block;">
  
</div>
<button  id="button" style='font-size: 200px;'>test</button>
var e = document.getElementById('red');

() => {

    if (sessionStorage.getItem('divColor') === '1'){
        e.style.display = "none";
    }
  else{
    e.style.display = "block";
  }
}



document.getElementById('button').addEventListener('click', function() {
  if (e.style.display === 'block') { 
  e.style.display = 'none';
     sessionStorage.setItem("divColor", 1);
  } else { 
  e.style.display = 'block';
  }
});

You could use cookie storage: Explanation: After changing the background in the function then save it inside a cookie and set the cookie expiring date, the expiring date might be one day or any time, and always check if the cookie is set if not prompt the client to change the page background, and once the cookie is set, use the color value stored in the cookie to set the page background, and once the browsers stores the cookie it will always be available for use, i think this is a better way to do such a thing on the website.可以使用cookie存储: 说明:在函数中更改背景后,将其保存在cookie中并设置cookie的到期日期,到期日期可能是一天或任何时间,如果没有提示,请始终检查cookie是否设置客户端更改页面背景,设置cookie后,使用cookie中存储的颜色值来设置页面背景,一旦浏览器存储cookie,它将始终可用,我认为这是更好的在网站上做这样的事情的方法。 Hope this explanation is useful希望这个解释有用

I figured out my problem!我想出了我的问题! It took all weekend but I did it (I'm new to JS so small wins mean a lot).花了整个周末,但我做到了(我是 JS 的新手,所以小胜利意味着很多)。

<button id="button" style="padding: 20px";>test</button>
  
<p id="text" style="font-size:200px; padding: 0; margin: 0; text-transform: uppercase; background-color:red;">change me</p>
var button = document.getElementById('button');

var text = document.getElementById('text');

var key = sessionStorage.getItem ('key');

var obj = JSON.parse(key);


if (key)
  {text.style.background = obj};

button.addEventListener('click', textChange);

function textChange (test) {
  if (text.style.backgroundColor === 'red') {
    text.style.backgroundColor = 'blue';
    sessionStorage.setItem ('key', JSON.stringify (text.style.backgroundColor));
  } else {
    text.style.backgroundColor = 'red';
     sessionStorage.removeItem ('key');
  } 
  }

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

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