2014-01-22 23 views
0

有一个小的要求,有一个iframe,然后是3个按钮。 一旦我们点击button1,url应该显示在iframe中,与第二个按钮相同。 这是我的代码,请你帮忙。使用javascript在Iframe中显示的网址

function testCoverage(){  
    window.frames['idIfrm'].document.location.href = 'http://google.com'; 
    } 
function testCoverage1(){ 
    window.frames['idIfrm'].document.location.href = 'http://yahoo.com'; 
} 

这里是JsFiddle

回答

1

请试试这个代码,它工作在我结束精细,

<div> 
    <button><a href="http://www.computerhope.com/banners/banner.gif" target="iframe_a">Banner1</a></button> 
    <button><a href="http://www.computerhope.com/banners/banner2.gif" target="iframe_a">Banner2</a></button> 
    <button><a href="http://www.computerhope.com/banners/banner3.gif" target="iframe_a" onclick="closeWindow();">Banner3</a></button> 
    <div>  
    <iframe src="http://www.computerhope.com/banners/banner3.gif" name="iframe_a" width="900" height="350px" id="iframe" frameborder="0" scrolling="no" style="margin-left:15px;margin-bottom:15px;border:none;"> 
    </iframe> 
    </div> 
</div> 
1

好,因为它被设置为运行JavaScript的onload,而不是在你的头上摆弄不运行。更改左上角的下拉菜单。

enter image description here

其次的问题,您应该设置的iframe,而不是文件位置的来源。

window.frames['idIfrm'].src = "foo.html"; 

第三个问题,你无能为力的是谷歌和雅虎不能放入iframe。你会得到错误

拒绝在该框架中显示“https://www.google.com/”,因为它设置 “X-框架 - 选项”到“SAMEORIGIN”。

+0

其实在这里我想显示的谷歌地图。 – Rajasekhar

+0

比使用[google map api](https://developers.google.com/maps/)! – epascarello

1

您应该使用src属性。你的代码看起来应该是这样

function testCoverage(){ 
     document.getElementById['idIfrm'].src = 'http://google.com'; 
    } 
    function testCoverage1(){ 
     document.getElementById['idIfrm'].src = 'http://yahoo.com'; 
    } 
+0

我试过这个,但没有运气。 – Rajasekhar