2014-01-22 40 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