简体   繁体   English

如何使用JavaScript从另一个页面更改一个页面的CSS

[英]How to change CSS of one page from another page using JavaScript

I have two web pages: 我有两个网页:

pageone.html and pagetwo.html pageone.htmlpagetwo.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. pageone.html ,有一个ID为change-color按钮当您单击该按钮时, pagetwo.html的背景颜色应该变为绿色。 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 在pageone.html中

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

In pagetwo.html 在pagetwo.html中

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

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

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