javascript - 如何使用 Javascript 链接到另一个页面并在该新页面上执行操作?
问题描述
我想在主页上使用一个按钮,当它链接到新页面时,会更改新页面的背景颜色。在代码中,我有两个按钮。一个是无聊的按钮。它链接到页面,不做任何其他事情。另一个是紫色按钮,它应该链接到同一页面,并更改背景颜色。
我已经尝试将两个页面链接到同一个脚本文档。我也尝试过使用“DOMContentLoaded”事件侦听器,它也不起作用。
主页代码:
<button id="boringBtn">Press this button to go to a new page and do nothing</button>
<button id="purpleBtn">Press this button to go to a new page and change the color</button>
<script src="link-script.js"></script>
链接脚本代码:
function newPage() {
window.location = "linked-page.html";
}
function newPageWithColor() {
window.location = "linked-page.html";
document.body.style.background = "purple";
}
document.getElementById("boringBtn").onclick = newPage;
document.getElementById("purpleBtn").onclick = newPageWithColor;
解决方案
推荐阅读
- java - 在数据库中存储没有重复的随机字符串
- python - 数据溢出到另一列
- replace - 如何替换 Tableau 交叉表中单元格中的特定值?
- asp.net-core - SignalR - 通过后台服务发送给客户端
- java - 在 Java 中逐行读取文本
- if-statement - Netlogo: Turtle checking if it can move into a valid target
- html - 克隆表行时,如何只克隆先前输入的一个值而不是全部?
- html - 如何正确实现标签控件的可访问性
- c# - 为 DbSet 创建扩展方法是一种不好的做法吗
? - ruby-on-rails - rails 活动记录返回错误的类类型