首页 > 解决方案 > 如何使用 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">&lt;/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;

标签: javascriptjqueryhtml

解决方案


推荐阅读