Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过单击第1页中的按钮更改第2页中html的颜色 **page1.html** 试试看 函数myFunction(){ document.querySelector(“.example”).style.backgroundColor=“红色”; } **page2.html** 带有class=“example”的标题_Javascript_Html - Fatal编程技术网

Javascript 如何通过单击第1页中的按钮更改第2页中html的颜色 **page1.html** 试试看 函数myFunction(){ document.querySelector(“.example”).style.backgroundColor=“红色”; } **page2.html** 带有class=“example”的标题

Javascript 如何通过单击第1页中的按钮更改第2页中html的颜色 **page1.html** 试试看 函数myFunction(){ document.querySelector(“.example”).style.backgroundColor=“红色”; } **page2.html** 带有class=“example”的标题,javascript,html,Javascript,Html,单击第1页中的按钮后,名为example的类的h2标记的背景色不会改变。一种可能的解决方案是使用本地存储,这些存储在同一域的页面上持久化。然后在目标页面中读取“onload”中的值 在第1页: **page2.html** <h2 class="example">A heading with class="example"</h2> 在第2页: function myFunction() { localStorage.setItem("exampleBGCo

单击第1页中的按钮后,名为example的类的h2标记的背景色不会改变。

一种可能的解决方案是使用本地存储,这些存储在同一域的页面上持久化。然后在目标页面中读取“onload”中的值

在第1页:

**page2.html**

<h2 class="example">A heading with class="example"</h2>

在第2页:

function myFunction() {
    localStorage.setItem("exampleBGColor", "red");
}

希望这能对您有所帮助。

要在其他页面中进行更改,无法直接从您的页面访问它

所以你可以用一些创新的方法来做

例如:

  • 您可以将参数传递到url中的另一个页面(“page2”),然后在 “page2”从查询参数读取它并设置元素的颜色

  • 您可以在localstorage中设置颜色,然后在new page read中设置颜色 从localstorage中选择并设置元素的颜色


您可以使用LocalStorage api实现类似的行为

第一页:

window.onload = function() {
    let BGcolor = localStorage.getItem("exampleBGColor");
    if(BGcolor) {
        document.querySelector(".example").style.backgroundColor = BGcolor;
        localStorage.removeItem("exampleBGColor");
    }
}

本地存储
函数setColor(){
setItem('currentColor','red');
}
变色
第二页:

<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Local Storage</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <script>
        function setColor() {
            localStorage.setItem('currentColor', 'red');
        }
    </script>
</head>
<body>
    <button onclick="setColor()">
        Change Color
    </button>
</body>
</html>

本地存储
window.addEventListener('storage',函数(e){
const div=document.getElementById('div');
div.style.backgroundColor=e.key=='currentColor'?e.newValue:null;
});
我的颜色会变的
使用
存储
事件侦听器允许第2页中的div响应性地更新其颜色。
您可以了解有关本地存储的更多信息。

正如@juanram0n、Ilia@Afzali和@dev_junwen所说,您可以通过使用JavaScript窗口本地存储属性来实现

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Local Storage</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <script>
        window.addEventListener('storage', function(e) {
            const div = document.getElementById('div');
            div.style.backgroundColor = e.key === 'currentColor' ? e.newValue : null;
        });
    </script>
</head>
<body>
    <div id="div">
        My Color Will Change
    </div>
</body>
</html>
*page1.html*
试试看
函数myFunction(){
//商店背景色
setItem(“bgColor”、“red”);
}

*页面2.html*
带有class=“example”的标题
document.querySelector(“.example”).style.backgroundColor=localStorage.getItem(“bgColor”);

JavaScript只能与运行它的页面中的内容交互。(或者在服务器上通过Ajax加载内容)。例外情况可能是,如果将page2作为page1的某种“子”加载到浏览器中,例如在iframe中,或从弹出窗口加载,是否有其他解决方案?除了我刚才列出的以外,不可以。想象一下,如果随机网页可以更新其他随机网页的内容,而这些网页恰好被加载到同一浏览器的另一个选项卡中。那将是一片混乱!黑客等将有一个现场一天。这是一件好事,这是不可能的。正如下面其他人所说,您可以使用各种方法在页面之间传递数据,但这假设您稍后加载page2,而您的代码似乎假设这两个页面都已加载到不同的选项卡中。如果你能弄清楚确切的情况,也许会有帮助
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Local Storage</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <script>
        window.addEventListener('storage', function(e) {
            const div = document.getElementById('div');
            div.style.backgroundColor = e.key === 'currentColor' ? e.newValue : null;
        });
    </script>
</head>
<body>
    <div id="div">
        My Color Will Change
    </div>
</body>
</html>
*page1.html*

<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
 // Store backgroundColor
 localStorage.setItem("bgColor", "red");
}
</script>


*page2.html*

<h2 class="example">A heading with class="example"</h2>
<script>
document.querySelector(".example").style.backgroundColor = localStorage.getItem("bgColor");
</script>