Javascript 如何使按钮单击其他页面上的按钮

Javascript 如何使按钮单击其他页面上的按钮,javascript,html,Javascript,Html,我在一个html页面上有一个按钮,但当单击时,我希望它在不同的html页面上激活一个不同的按钮。这可能吗?只有在第一页实际生成第二页时(通过window.open())才可能。否则,您将无法访问其他窗口中的文档 下面是一个例子: var newWin = window.open("other page in my domain"); var otherButton = newWin.document.querySelector("selector to find button"); otherB

我在一个html页面上有一个按钮,但当单击时,我希望它在不同的html页面上激活一个不同的按钮。这可能吗?

只有在第一页实际生成第二页时(通过
window.open()
)才可能。否则,您将无法访问其他窗口中的文档

下面是一个例子:

var newWin = window.open("other page in my domain");
var otherButton = newWin.document.querySelector("selector to find button");
otherButton.click();

尽管这个解决方案可以工作,但它需要将一个值从一个页面传递到另一个页面。它无法将页面链接在一起。如果你想调查的话

第1页

HTML

第2页

HTML


仅在某个域中,并且当其中一个页面从另一个页面打开时。单击事件能否在单独的浏览器选项卡中触发单击事件?不。这是为了防止恶意行为(黑客攻击)。你能把自己的JavaScript放在第二页吗?就是这样。第一个按钮链接到第二个页面,我希望单击第一个按钮也可以单击第二个页面上的按钮。@Ebower否,链接到另一个页面不是我要说的,也不会起作用。第一个页面必须以编程方式创建第二个窗口,其中第二个页面位于其中。
<button id="activate">Activate other page</button>
document.querySelector("#activate").addEventListener("click", function(){
    location.href = "page2.html?button=activate"; //mind the querystring here.
});
<button id="toactivate" disabled>disabled button</button> 
var activateButton = location.search.slice(1); //querystring | get rid of the ?
activateButton.split(/&/);
activateButton.forEach(function(value){
    var splitted = value.split("=");
    if (splitted[0] == "button" && splitted[1] == "activate")
    {
       document.querySelector("#toactivate").removeAttribute("disabled");
    } 
});