最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to keep background color changes on page reload? - Stack Overflow

programmeradmin5浏览0评论

I have a table which contain cells that when clicked, will change the cell's background color to green. When I reload the page, however, the background color reverts back to its original color; white. Is there any way I can prevent it from reverting? To just keep it green after I clicked it?

JS:

        function eight(){
            var eight = document.getElementById("eight");
            eight.style.backgroundColor = "green";
        }

HTML:

        <td id="eight" onclick="eight(); ">
            <a>8</a>
            <br>
            <center>-</center>
            <br>
            <center>-</center>
            <hr>
            <center>-</center>
            <br>
            <center>-</center>
        </td>

*Ignore the content inside the <td> tags.

I have a table which contain cells that when clicked, will change the cell's background color to green. When I reload the page, however, the background color reverts back to its original color; white. Is there any way I can prevent it from reverting? To just keep it green after I clicked it?

JS:

        function eight(){
            var eight = document.getElementById("eight");
            eight.style.backgroundColor = "green";
        }

HTML:

        <td id="eight" onclick="eight(); ">
            <a>8</a>
            <br>
            <center>-</center>
            <br>
            <center>-</center>
            <hr>
            <center>-</center>
            <br>
            <center>-</center>
        </td>

*Ignore the content inside the <td> tags.

Share Improve this question asked Aug 13, 2016 at 3:49 cosmocosmo 7613 gold badges16 silver badges44 bronze badges 2
  • 1 You can have a boolean isLoaded and store it in local storage. developer.mozilla/en-US/docs/Web/API/Window/localStorage – Yasin Yaqoobi Commented Aug 13, 2016 at 3:51
  • localStorage, sessionStorage and document.cookie are your choices in JavaScript. document.cookie works on all Browsers. I remend using a Library if using document.cookie, due to Client Side (Browser) limitations. If you are using HTML5 stuff you're fine with localStorage and sessionStorage. – StackSlave Commented Aug 13, 2016 at 4:23
Add a ment  | 

3 Answers 3

Reset to default 5

You have to store the color value in Client side Cookie or Server Side Session. Modern browsers supports localStorage also..

Try this Code..

function changeBackground() {
        if (sessionStorage.getItem('colour')) {
            document.body.style.backgroundColor = sessionStorage.getItem('colour');
        }else{
            document.body.style.backgroundColor =  "#BB0A21";
            sessionStorage.setItem('colour', "#BB0A21");
        }
    }

  // then you'll need to call your function on page load
  changeBackground();

As @Yasin says

use localstorage.take global variable

first add a class to all the cells

suppose color.then

 var cells=document.getElementsByClassname('color');
        var colours=[];
        if(localstorage.getItem('colours'))
        {
        colours = JSON.parse(localstorage.getItem('colours'));  // if alreay there 

        for(i=0;i<cells.length;i++)
        {
           for(j=0;j<colours.length;j++)
             {
                if(cells[i].getAttribute('id')==colours[j].id)
                {
                   cells[i].style.backgroundColor = colours[j].colour;
                 }
               }
        }


        }

        function eight(){
                    var eight = document.getElementById("eight");


                eight.style.backgroundColor = "green";
                 colours.push({id:'eight',colour:'green'});
                 localstorage.setItem('colours',JSON.stringify(colours));


   }

I have tried a way to achieve the request you provided,sessionStorage should be the best solution for you,cookie can do it anyway,but html5 supports Web Storage,here are my demo about that.

<html DOCTYPE>
<head>
<title>hello world</title>
</head>
<body>
<style type="text/css">
#t1{
	background-color: aliceblue;
}
</style>
<table id="t1" onclick="eight();">
<tr>
<td>hello world</td>
</tr>
<tr><td>hello world</td></tr>
<tr><td>hello world</td></tr>
</table>
</body>
<script type="text/javascript">
(function(){
	var t = document.getElementById("t1");
	window.addEventListener("load", function(){
		var color = window.sessionStorage.getItem("tableColor");
		if(color == 'undefined'){
			t.style.backgroundColor = "aliceblue";
		}else if(color == "green"){
			t.style.backgroundColor = color;
		}else{
			alert("another color you have chosen");
		}
	}, false);
})()
function eight(){
	var eight = document.getElementById("t1");
	eight.style.backgroundColor = "green";
	window.sessionStorage.setItem("tableColor", "green");
}
</script>
</html>

and it will ok after you click the table and reload the page.the color will remain the previous status and only if you close your browser and reopen this page again, the table background will change to the origin status.

发布评论

评论列表(0)

  1. 暂无评论