简体   繁体   English

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

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

I have a button on one html page, but when clicked I want it to activate a different button on a different html page. 我在一个html页面上有一个按钮,但是当我单击它时,我希望它在不同的html页面上激活一个不同的按钮。 Is this possible? 这可能吗?

This is only possible if the first page actually generates the second page (via window.open() ). 仅当第一页实际生成第二页时(通过window.open() ),才有可能。 Otherwise, you won't have any way to access the document in the other window. 否则,您将无法在其他窗口中访问文档。

Here's an example: 这是一个例子:

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

This solution will work though, but it requires passing a value from one page to another. 虽然此解决方案可以工作,但是需要将值从一页传递到另一页。 It cannot link pages together. 它无法将页面链接在一起。 If you want that look into websockets . 如果您愿意,请查看websockets

Page 1 第1页

HTML HTML

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

JavaScript JavaScript的

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

Page 2 第2页

HTML HTML

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

JavaScript 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