Javascript 使用按钮隐藏/显示 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14113850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:45:46  来源:igfitidea点击:

Hide/Show iframes with a button

javascripthtmlcss

提问by Craig

I have a webpage with 2 iframes, 1 underneath the other. I would like both iframes hidden when a user first clicks on the webpage. There will be 2 buttons, 1 above each iframe and the user must click on the button to show the iframe. Also I want it so if iframebutton1 is pressed then iframe2 will be hidden (if it's showing) and visa versa.

我有一个带有 2 个 iframe 的网页,1 个在另一个下方。当用户第一次单击网页时,我希望隐藏两个 iframe。将有 2 个按钮,每个 iframe 上方有 1 个,用户必须单击该按钮才能显示 iframe。我也想要它,所以如果 iframebutton1 被按下,那么 iframe2 将被隐藏(如果它正在显示),反之亦然。

Here is my code:

这是我的代码:

jsfiddle.net/darego/Z62P7/

jsfiddle.net/darego/Z62P7/

回答by Sethen

Here is the code that I would recommend using:

这是我建议使用的代码:

function hideToggle(button, elem) {
  $(button).toggle( function () {
    $(elem).hide();
  },function () {
    $(elem).show();
  });
}

hideToggle(".button1", ".iframe1");
hideToggle(".button2", ".iframe2");

Here is the updated working fiddle: Click here

这是更新的工作小提琴:单击此处

This just uses a simple hide/show function so you can reuse it again and again.

这只是使用一个简单的隐藏/显示功能,因此您可以一次又一次地重复使用它。

回答by HMarioD

To show or hide iframes:

要显示或隐藏 iframe:

document.getElementById("yourIFrameid").style.display = "none"; //hides the frame
document.getElementById("yourIFrameid").style.display = "block"; //shows the frame