简体   繁体   中英

How to change CSS of one page from another page using JavaScript

I have two web pages:

pageone.html and pagetwo.html

In pageone.html , there is a button with an id of change-color When you click that button, the background color of pagetwo.html is supposed to turn green. But that isn't happening. Can someone help me?

 function changeColor() { //code var pageTwo = 'pagetwo.html'; pageTwo.style.backgroundColor = "green"; } 
 <button type="button" id="change-color" onclick="changeColor">Change Background Color</button> 

You can try like below

In pageone.html

 window.localStorage.setItem("bg", "")
 function changeColor() {
         window.localStorage.setItem("bg", "green");
 }

In pagetwo.html

<script>
var bg = window.localStorage.getItem("bg");
if(typeof bg!= 'undefined' && bg != ""){
    //Set your background using bg variable
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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