iframe父頁面與子頁面之間的傳值問題


一、父頁面給iframe中的子頁面傳值,把值寫入子頁面的文本框里

father.html

<script language="javascript" src="http://www.aspbc.com/js/jquery.js" type="text/javascript"></script> 
<script type="text/javascript"> 
function fuzhi() 

    $('#son').contents().find("#b").val("父頁面傳過來的值!");  

</script> 

 

<iframe id="son" name="son" src="son.html" width="400" height="200"></iframe><br /> 
<input type="button" value="給子頁面表單中id為b的文本框賦值" onclick="fuzhi()" /> 

son.html

<form name="form2"><input type="text" name="b" id="b" /></form>

二、子頁面如何調用父頁面中的函數

father.html

<script language="javascript" src="http://www.aspbc.com/js/jquery.js" type="text/javascript"></script> 
<script type="text/javascript"> 
function fun() 

    alert('這是父頁面中的函數彈出窗口哦!'); 

</script>  
<iframe id="son" name="son" src="son.html" width="400" height="200"></iframe>

son.html

<script type="text/javascript"> 
function diaoyong() 
{     
    $(window.parent.fun());  //調用父頁面函數 

</script> 
<form name="form2"> <input name="btn1" type="button" onclick="diaoyong()" value="調用父頁面中的函數" /></form>

三、iframe中的子頁給父頁面傳值

father.html

<script language="javascript" src="http://www.aspbc.com/js/jquery.js" type="text/javascript"></script> 
<div id="messagediv">test</div> 
<iframe id="son" name="son" src="son.html" width="400" height="200">
</iframe>

son.html

<script type="text/javascript"> 
function fuzhi() 

    $(window.parent.$("#messagediv").html("子頁面賦過來的值")); 

</script> 
<form name="form2"><input name="btn1" type="button" onclick="fuzhi()" value="給父頁中id為messagediv的元素賦值" /></form>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM