2016-12-30 146 views
0

所以,我有我的iframe內的按鈕,這帶來了使用的代碼父頁面上的一個div:彈出的IFRAME(多)在父頁面DIV

<script type='text/javascript'>  
function showDiv() { 
    window.parent.document.getElementById('coreradio').style.display = "block";; 
} 
</script> 
<input type='button' class='radiobutton' value='Core' onclick="showDiv('#coreradio')"> 

和我父頁面的DIV是:

<div id="coreradio" style="display:none"> 

這適用於父頁面上的一個DIV完全正常的,但如果我再添加載則只打開第一個,例如:

裏面的iframe:

<script type='text/javascript'>  
function showDiv() { 
    window.parent.document.getElementById('coreradio').style.display = "block";; 
} function showDiv() { 
    window.parent.document.getElementById('poppunkradio').style.display = "block";; 
} 
</script> 
<input type='button' class='radiobutton' value='Core' onclick="showDiv('#coreradio')"> 
<input type='button' class='rbutton' value='Core' onclick="showDiv('#poppunkradio')"> 

父窗口:

<div id="coreradio" style="display:none"> 
<div id="poppunkradio" style="display:none"> 

這導致只有一個負載不管你按哪個按鈕,這裏是一個空白頁

http://beta.musiccase.net/forums/iframe/

在工作示例我可能會錯過像錯誤的ID或某事那樣簡單的事情,但這讓我難以接受現在,所以任何幫助都會很棒!

+1

您不能聲明具有相同名稱的多個函數。他們互相烘烤 –

+0

你應該使用這樣的東西: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage – Krunal

+0

你如何給函數不同的名字? –

回答

0

你想這樣的:

function showDiv(id) { 
    window.parent.document.getElementById(id).style.display = "block"; 
} 

使用這樣的:

showDiv("coreradio"); 
showDiv("poppunkradio"); 

裏面一個按鈕:

<button onclick='javascript:showDiv("coreradio")'>Start Core Radio</button> 

關於函數:

函數是一組聲明。要執行這些,您需要給他們打電話:

function a(){ 
    alert(" a");//statement in a function 
}//function declared 
a();//function called 
a();//function called again 
+0

它根本不起作用,如果我把一個id放在showdiv(id) –

+0

你在哪裏「放」了id。你需要**調用**這個id的函數,就像我的例子 –

+0

我想你不明白什麼是函數 –