繁体   English   中英

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

[英]How to make a button click a button on a different page

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

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

这是一个例子:

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

虽然此解决方案可以工作,但是需要将值从一页传递到另一页。 它无法将页面链接在一起。 如果您愿意,请查看websockets

第1页

HTML

<button id="activate">Activate other page</button>

JavaScript的

document.querySelector("#activate").addEventListener("click", function(){
    location.href = "page2.html?button=activate"; //mind the querystring here.
});

第2页

HTML

<button id="toactivate" disabled>disabled button</button> 

JavaScript的

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");
    } 
});

暂无
暂无

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

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