我如何更改' src'来自另一个页面的iframe的点击?

时间:2016-12-09 10:32:18

标签: javascript jquery html

我有两个html网页1.html2.html

1.html上有两个按钮。

  • 一键链接到Google
  • 一键链接到Facebook

iframe中有2.html

如何通过点击iframe src上的按钮更改2.html上的1.html

3 个答案:

答案 0 :(得分:0)

你好试试......

<button onclick="return (function(){
   var _iframe = document.getElementById('iFrameId');
   _iframe.src = '1.html';
})();">Page 1</button>

<button onclick="return (function(){
   var _iframe = document.getElementById('iFrameId');
   _iframe.src = '2.html';
})();">Page 2</button>

答案 1 :(得分:0)

您可以使用以下

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script>
            function changeSrc(loc) {
                document.getElementById('iframeId').src = loc;
            }
      </script>
    </head>
    <body>
        <button onclick="changeSrc('http://en.wikipedia.org')">Wikipedia</button>
        <button onclick="changeSrc('http://www.bing.com')">Bing</button>
        <iframe id="iframeId" src="" width="100%" height="100%"></iframe>
    </body>
</html>

答案 2 :(得分:0)

您可以使用HTML而不需要CSS或JS / jQ:

  • 使用<a>创建一个href,其中包含所需目的地的网址。
  • 使用iframe target属性的值添加name属性。
顺便说一句,谷歌和Facebook破坏了iframe,因此示威活动有不同的网站。

PLUNKER

<!DOCTYPE html>
<html>

<head>
  <style>
    iframe {
      width: 97vw;
      height: 65vh;
    }
  </style>
</head>

<body>
  <fieldset>
    <button><a href='http://www.w3schools.com/' target='ifrm1'>W3Schools</a>
    </button>
    <button><a href='http://plainjs.com' target='ifrm1'>PlainJS</a>
    </button>
  </fieldset>
  <iframe name='ifrm1' src='about:blank'></iframe>

</body>

</html>

相关问题