一个 iframe 上的按钮以显示另一个 iframe

Button on one iframe to display another iframe

本文关键字:iframe 显示 另一个 一个 按钮      更新时间:2023-09-26

我有一个网页,上面有两个iframe:

<iframe src="Account.php" class="iframe" id="iframe1"></iframe>
<iframe src="Register.php" class="iframe" id="iframe2"></iframe>

在第一个 iframe 上,我有一个按钮

<input type="button" value="Subscribe" id="btn"/>

当我单击此按钮时,我想显示另一个当前隐藏的iframe"#iframe1",如下所示: $('#iframe1').show(); 显然,问题是要显示的按钮和 iframe 是不同的文档/页面。

任何想法,如果可能的话,如果没有jQuery的.load函数,我该如何实现这一目标?谢谢!

从第一个 iframe 开始,将 click 事件绑定到主窗口上的函数。

// in main window, define the function to show iframe 2
function showFrame2(e) {
   $('#iframe2').show();
}
// in frame 1, bind to that function by traversing to the parent document
$('#btn').on("click", function(e) {
  window.parent.showFrame2(e);
});

未经测试,但它可能会起作用。