html - 使用按钮显示和隐藏带有iframe的特定div

标签 html css iframe

我只是想为家庭成员做网站。我已经完成了带有 css 的基本 html,但是有一个问题。我需要在 php 中实现链接到两种表单的页面更改 iframe。现在我想出了更改 href 的代码,但是这些 iframe 具有不同的高度,所以我想将它们都关闭到 div 中并用按钮显示第一个或第二个。

这是目前的代码:

    <section id="predplatne" class="clearfix">
  <div class="predplatne_in">
    <h2>Text</h2>
    <div class="vyber">
      <button class="button"><a href="http://www.php" target="frame">1</a></button>
      <button class="button"><a href="http://www.php2" target="frame">2</a></button>
    </div>
  <div class="box">
    <div class="in">
      <iframe allowfullscreen frameborder="0" height="1700" id="frame" name="frame" src="http://www.php" width="480"></iframe> <a href="http://www.php" target="frame"></a> <a href="http://www.php2" target="frame"></a>
    </div>
  </div>
</div>
  </div>   

最佳答案

您可以为此使用 JS。据我了解,您想显示 <iframe>什么时候点击按钮?

首先,创建您的 <iframe> s 并设置它们的两个 CSS display值为 none ,像这样:

<div class="iframes">
  <iframe allowfullscreen style="display: none" frameborder="0" height="300" width="480" name="frame1" src="http://example.com"></iframe>
  <iframe allowfullscreen style="display: none" frameborder="0" height="300" width="240" name="frame2" src="http://thedragonring.me"></iframe>
</div>

然后,添加一些<button>在他们的 onclick 中有一个小脚本将切换是否为 display 的属性对应<iframe>的属性设置为 noneblock ,像这样:

<div class="buttons">
  <button onclick="
    const el = document.querySelector('.iframes [name=frame1]');
    el.style.display = el.style.display === 'none' ? '' : 'none';
  ">
    Frame 1 (example.com)
  </button>
  <button onclick="
    const el = document.querySelector('.iframes [name=frame2]');
    el.style.display = el.style.display === 'none' ? '' : 'none';
  ">
    Frame 2 (thedragonring.me)
  </button>
</div>

或者,您可以添加一个按钮来在 2 个 iframe 之间切换:

<button onclick="
  const frame1 = document.querySelector('.iframes [name=frame1]'),
    frame2 = document.querySelector('.iframes [name=frame2]'),
    prev = frame1.style.display;
  frame1.style.display = prev === 'none' ? '' : 'none';
  frame2.style.display = prev === 'none' ? 'none' : '';
">
  Toggle
</button>

基本上,所有这些 JS 片段所做的都是反转 display目标的属性 <iframe> .你可以看到一个演示@http://jsfiddle.net/TheDragonRing/4bzrvnyh/

关于html - 使用按钮显示和隐藏带有iframe的特定div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51375488/

相关文章:

html - float div 中的内容导致下面的 div 下降

html - 按钮宽度问题

css - 限制背景滑过屏幕的最左边缘

php - iFrame - 隐藏 url 和 session 不工作

javascript - 2个Javascript动画

javascript - JS 不会出现在带有 HTML 的浏览器中

javascript - 访问 jQuery .each() 循环之外的变量

python - 为什么在浏览器开发工具和 BeautifulSoap/Postman 中获取不同的数据?

javascript - Web 编程中类似 "Stay on Top"的东西?

javascript - 使用 JavaScript 调整 Tumblr 博客上嵌入的视频大小