2014-02-08 33 views
0

我正在處理一個網頁,我需要能夠單擊在父頁面中更改iframe的按鈕。我認爲這將是一個簡單的任務,但無論我做什麼,按鈕都會打開一個新窗口,而不是打開iframe中的頁面。該頁面看起來很完美,iframe加載,但當我點擊父窗口中的按鈕來更改它在新選項卡中打開的頁面。這裏是我正在使用的完整代碼(減去更改後的網頁名稱),如果有人能指出我做錯了請。如何更改iframe頁面,而不使用使用CSS的外部鏈接更改父頁面

<head> 
<style> 
    body 
    { 
    background-image:url(bg4.jpg); 
    background-repeat:no-repeat; 
    background-attachment:fixed; 
    background-position:top; 
    background-size:100%; 
    } 
    p.pos_fixed 
    { 
    position:fixed; 
    top:98px; 
    right:298px; 
    } 
</style> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Stupid webpage that dont work</title> 
</head> 

<body> 
    <p class="pos_fixed"> 
    <iframe id="MSL" src="http://www.yahoo.com" width="975" height="800"> 
    </iframe> 
    <center> 
     <a href="http://www.google.com" target="MSL"> 
     <img src="MSL.jpg" alt="MSL" width="42" height="42"></a> 
    </center> 
    </p> 

</body> 
</html> 

回答

0

爲了讓你的鏈接到目標的iframe,你必須添加屬性「名稱」這樣的框架,無關「身份證」

<iframe id="MSL" name="MSL" src="http://www.yahoo.com" width="975" height="800"> 
</iframe> 

另外,你的DOM結構非常不好,你不應該在段落內嵌入iFrames。簡而言之,錨元素中的目標屬性需要與iFrame中的名稱屬性相匹配,除非它是諸如「_blank」,「_self」,「_parent」或「_top」之類的保留屬性。

+0

我沒有名稱=「MSL」沒有工作我也試過同時名稱和ID;沒有工作 – user3288347

+0

只要你不使用谷歌或雅虎作爲測試頁面,它應該工作,那些返回一個頭,不允許iFrames加載它們。所以請儘量使用cnn.com和bbc.co.uk,但將iframe名稱添加到iframe中確實有效。 – joseeight

+0

看到這個小提琴,看到它的作品很好:http://jsfiddle.net/2JjVz/(滾動到底部的按鈕) – joseeight