简体   繁体   English

如何使用 javascript 在其他页面上编辑样式?

[英]How can I edit style on other page with javascript?

I'm trying to make a function that will changes the page and marks the selected div (the div is in the other page), The problem is I the function cuts off because its causing the page to refresh.我正在尝试制作一个 function 来更改页面并标记选定的 div(div 在另一页中),问题是我 function 会中断,因为它会导致页面刷新。

Here is the javascript code:这是 javascript 代码:

    function movePack1(){
    var pac1 = document.getElementById("pack1");
    window.location.href="enclosures.html";
    pac1.style.boxShadow = "0px 0px 5px blue";
}

You cannot do this in the way you are attempting with client side JavaScript.您不能以您尝试使用客户端 JavaScript 的方式执行此操作。

If you are trying to execute a dynamic action on a page when it loads, consider passing a GET parameter and then doing something with that on the new page.如果您在加载页面时尝试在页面上执行动态操作,请考虑传递 GET 参数,然后在新页面上执行此操作。

window.location.href="enclosures.html?action=shadowpac1";

Then in enclosures.html you would write JavaScript which checks for the "action" GET parameter.然后在enclosures.html 中编写JavaScript 来检查“action”GET 参数。 If the "action" parameter is equal to "shadowpac1", then you apply your styles.如果“action”参数等于“shadowpac1”,则应用 styles。

Look at the following two posts:看下面两个帖子:

  1. How to parse out a GET parameter - How to retrieve GET parameters from JavaScript如何解析 GET 参数 - 如何从 JavaScript 检索 GET 参数
  2. How to use DOMContentLoaded to run the function doing the parsing-and-styling after the DOM is loaded: https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event如何使用 DOMContentLoaded 在加载 DOM 后运行 function 进行解析和样式设置: https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event

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

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