2017-04-11 21 views
0

嗨通過調用父鏈接頁面值到它的iframe鏈接頁面 示例代碼傳遞價值,它的iframe我要傳遞的價值是如何從父母使用javascript

<html> 
    <head> 
     <title> 
     </title> 
     <script> 
      function sendvalue(v){ 
       alert(v); 
      } 
     </script> 
    </head> 
    <body> 
     <form id="f"> 
      <input type="button" value="123" onclick="sendvalue(this.value)"> 
     </form> 
     <iframe id="frame1" src="a.html" width="500px;"></iframe> 
    </body> 
</html> 

a.html

<html> 
    <head> 
     <title> 
     </title> 
     <script> 

     </script> 
    </head> 
    <body> 
     <h3> Frame</h3> 
     <form id="myform"> 
      Req value <input type="text" name="a" id="a"> 
     </form> 
    </body> 
</html> 

當我調用sendvalue函數時,值應該傳遞給iframe鏈接頁面,用於輸入字段的形式。 這是如何實現的。

+0

[從窗口傳遞值給iframe]可能的副本(http://stackoverflow.com/questions/536538/pass-value-to-iframe-from-a-window) –

+0

可能的http:// stackoverflow .COM /問題/ 935127 /如何對訪問父iframe的從JavaScript的 –

回答

0
<html> 
<head> 
<title> 
</title> 
<script> 
function sendvalue(v){ 
document.getElementById('va').value = v; 
window.frames['framename'].document.getElementById("a").value = v 


} 
</script> 
</head> 
<body> 
<form id="f"> 
<input type="button" value="123" onclick="sendvalue(this.value)"> 
<input type="text" name="va" id="va"> 
</form> 
<iframe name="framename" src="a.html" width="500px;"></iframe> 
</body> 
</html> 

a.html

<html> 
<head> 
<title> 
</title> 
</head> 
<body> 
<h3> Frame</h3> 
<form id="myform"> 
Req value <input type="text" name="a" id="a"> 
</form> 
</body> 
</html> 
0

它 添加到您的最好辦法IFRAME

<iframe id="frame1" src="a.html?data=SomeData" width="500px;"></iframe> 

和iframe中你可以通過varible

var MYDATA = location.href.split('=')[1].split('&'); 
獲取信息

(您將看到MYDATA = SomeData

0

您可以使用postMessage以便在2個iframe之間進行通信。

在您需要iframeEl.contentWindow.postMessage(value as a string, can be json.stringified value)

搶iframe的窗口在您需要添加message類型的事件,像孩子框架的父框架:

window.addEventListener('message', funcrion(dataPassed) { 
    //Your logic here. 
}) 

欲瞭解更多信息read that