jquery iframe 父子互操作

父页面

View Code
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(
function(){
$(
"#btn").click(function(){
    
var sondocument = $(window.frames[0].document)
    alert(sondocument.find(
"#smsg").html());
})});
</script>
</head>
<body>
<div>below me it is an iframe</div>
<iframe id="myiframe" src="son.html" width="300px;" height="500px;"></iframe>
<div id="mm">if you see me through alert from an son's button,you win!</div>
<input type="button" id="btn" value="click me "/>


</body>
</html>

 子页面

View Code
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(
function(){
$(
"#btn").click(function(){
    
var mm = $('#mm', window.parent.document)
    alert(mm.html())    
})});
</script>
</head>
<body>
<input type="button" id="btn" value="click me "/>
<div>click the button above to get parent element's innerHTML</div>
<div id="smsg">if you see me in the alert msg,you win</div>
</body>
</html>



原文地址:https://www.cnblogs.com/applesuch5/p/2150128.html