Javascript 在 iframe 中更改页面的按钮或链接

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

Button or Link That Changes Page in Iframe

javascripthtmlbuttoniframehyperlink

提问by user1373771

I wish to make a link or button in a page that changes the page the iframe is on. It would be a local page:

我希望在页面中创建一个链接或按钮来更改 iframe 所在的页面。这将是一个本地页面:

idreesinc.com/iframe.html

idreesinc.com/iframe.html

and you can see what I have already here:

你可以看到我已经在这里:

idreesinc.com/research

idreesinc.com/research

Help would be much appreciated as i have been looking for an answer for ages. Thanks!

帮助将不胜感激,因为我一直在寻找答案多年。谢谢!

回答by ofermc

You can simply make an link with its target to the iframe name as follows:

您可以简单地将其目标链接到 iframe 名称,如下所示:

<iframe name="demo" src="example.php"></iframe>

Your link in the parent will be:

您在父级中的链接将是:

<a href="newpage.html" target="demo">Change Link</a>

回答by Danilo Valente

<script>
function setURL(url){
    document.getElementById('iframe').src = url;
}
</script>
<iframe id="iframe" src="idreesinc.com/research.html" />
<input type="button" onclick="setURL('URLHere')" />

回答by Tina CG Hoehr

I'm using jQuery.

我正在使用 jQuery。

$("#mybutton").click(function(){$("#myiframe").attr("src", "http://www.yahoo.com/")});?

$("#mybutton").click(function(){$("#myiframe").attr("src", "http://www.yahoo.com/")});?

http://jsfiddle.net/ws2hm/1/

http://jsfiddle.net/ws2hm/1/

 #myiframe { width: 300px; height: 300px }

$("#mybutton").click(function(){$("#myiframe").attr("src", "http://www.yahoo.com/")});?

Text on main page
<button id="mybutton">Swap</button>
<iframe id="myiframe" src="http://www.bing.com/"></iframe>?