简体   繁体   English

使用按钮在iframe中显示和隐藏特定的div

[英]Revealing and hiding specific div with iframe inside with button

I´m just trying to do website for family member. 我只是想为家人提供网站。 I´ve got base html with css done, but there is one issue. 我已经用CSS完成了基本的html,但是有一个问题。 I need to implement to page changing iframe that is linked to two kinds of forms in php. 我需要实现链接到php中两种形式的页面更改iframe。 Right now I´ve figured out code for change hrefs, but these iframes have different heights so I want to close both of them into divs and reveal first or second with buttons. 现在,我已经找到了更改href的代码,但是这些iframe具有不同的高度,因此我想将它们都关闭到div中,并使用按钮显示第一或第二个。

Here is the code so far: 这是到目前为止的代码:

    <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>   

You can achieve with some methods below 您可以通过以下一些方法来实现

 #if1, #if2{ display:none; } #btn1:active ~ div #if1, #btn2:active ~ div #if2{ display:block; } #rd1:checked ~ div #if1, #rd2:checked ~ div #if2{ display:block; } #cb1:checked ~ div #if1, #cb2:checked ~ div #if2{ display:block; } 
 <!-- Reveal with active button --> <button id="btn1">Reveal Frame 1</button> <button id="btn2">Reveal Frame 2</button> <br> <!-- Reveal with checked radio --> <input type="radio" id="rd1" name="reveal"> <label for="rd1">Reveal Frame 1</label> <input type="radio" id="rd2" name="reveal"> <label for="rd2">Reveal Frame 2</label> <br> <!-- Reveal with checked checkbox --> <input type="checkbox" id="cb1"> <label for="cb1">Reveal Frame 1</label> <input type="checkbox" id="cb2"> <label for="cb2">Reveal Frame 2</label> <div> <iframe width="100px" id="if1" src="http://google.com">Test</iframe> <iframe width="200px" id="if2" src="http://amazon.com">Test</iframe> </div> 

The easiest way is use javascript than just html and css 最简单的方法是使用JavaScript,而不仅仅是HTML和CSS

Hope it helps 希望能帮助到你

You can use JS for that. 您可以使用JS。 From what I've understood, you want to show an <iframe> when the button is clicked? 据我了解,您想在单击按钮时显示<iframe>吗?

First off, create your <iframe> s and set both of their CSS display values to none , like so: 首先,创建您的<iframe>并将其两个CSS displaynone设置为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>

Then, add some <button> s with a little script in their onclick attributes that will toggle whether the display attribute of the corresponding <iframe> is set to none or block , like so: 然后,在它们的onclick属性中添加一些带有小脚本的<button> ,它们将切换相应<iframe>display属性设置为none还是block ,如下所示:

<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>

Alternatively, you could add a single button that will toggle between the 2 iframes: 另外,您可以添加一个按钮,在两个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>

Basically, all these snippets of JS do is inverse the display attribute of the target <iframe> . 基本上,JS的所有这些摘要都与目标<iframe>display属性相反。 You can see a demo @ http://jsfiddle.net/TheDragonRing/4bzrvnyh/ 您可以在http://jsfiddle.net/TheDragonRing/4bzrvnyh/查看演示

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

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