使用 javascript 函数在 iFrame 中加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32790086/
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-23 14:12:51  来源:igfitidea点击:

Using a javascript function to load a page in an iFrame

javascriptiframe

提问by MistaBoodadee

I'm trying to use a Javascript function to load a web page in an iframe named "cake". How can I do that?

我正在尝试使用 Javascript 函数在名为“cake”的 iframe 中加载网页。我怎样才能做到这一点?

<html>
  <head>
  </head>
  <body>
    <button onclick=[function]>Load page in iframe</a>
  </body>
</html>

回答by Sanket Bajoria

function changeUrl() {
    var site = "http://www.w3schools.com";
    document.getElementsByName('iFrameName')[0].src = site;
}
    <html>
      <head>
      </head>
        <body>
          <button onclick="changeUrl()">Load page in iframe</button>
          <iframe name="iFrameName"></iframe>
        </body>
    </html>

回答by MistaBoodadee

This worked:

这有效:

<html>

<head>


<script type="text/javascript">

function changeUrl() {
    var site = "1.wav";
    document.getElementsByName('cake')[0].src = site;
}


</script>


</head>

<body>


<center>

<iframe src="http://www.w3schools.com" height=400 width=400 frameborder=0 name = "cake" style =""></></iframe>

<br>

<br>

<button onclick="changeUrl()">Load page in iframe</button>

</center>


</body>

</html>