如何通过单击第 1 页中的按钮更改第 2 页中 html 的颜色 [英] How to change color of html in page 2 by clicking the button in page 1

查看:25
本文介绍了如何通过单击第 1 页中的按钮更改第 2 页中 html 的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**page1.html**

     <button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
  document.querySelector(".example").style.backgroundColor = "red";
}
</script>

**page2.html**

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

类名为example的h2标签的背景颜色在点击第1页的按钮后不会改变.

The background color of the h2 tag with class named example won't change after clicked the button in page 1.

推荐答案

对于在另一个页面中进行的更改,无法直接从您的页面访问它.

For making a change in another page it's not possible to directly access it from your page.

所以你可以通过一些创新的方式来做到这一点!

So you can do it from some innovative ways !

例如:

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

  • You can pass a parameter to another page ('page2') in your url and then in 'page2' read it from query params and set the color for element.

可以在localstorage中设置颜色,然后在新页面读取它来自 localstorage 并设置元素的颜色.

You can set the color in the localstorage and then in new page read it from localstorage and set the color of element.

这篇关于如何通过单击第 1 页中的按钮更改第 2 页中 html 的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆