Codomain jQuery of works with DOM of across iframe

  • 2020-03-30 00:52:46
  • OfStack

Frame is still popular, and many places use it for special cases. For example, traditional upload, select under ie6, proxy, cross-domain, and so on. Today, I'll give you a brief overview of the operations that span iframes, primarily using jQuery to manipulate aspects of DOM structure.


<iframe src="a.php" id="aa"></iframe> 
<iframe src="b.php" id="bb"></iframe> 
<input type="button" id="read-aa" value=" read iframe #aa"> 
<input type="button" id="write-aa" value=" write iframe #aa">


$('#read-aa').click(function() 
{ 
    var v=$('#aa').contents().find('body').html(); 
    alert(v); 
}); 
$('#write-aa').click(function() 
{ 

    $('#aa').contents().find('div').append('<hr> This is a index.php operation aa.php Written content '); 
});

The main method is contents(), which reads the iframe.

2. Iframe cross-parent frame operation iframe


<!DOCTYPE html> 
<meta charset="utf-8"> 
<title>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</title> 
<script src="public/jQuery.js"></script> 
<script> 
$(function() 
{ 
    $('#read-parent-aa').click(function() 
    { 
        var v=$('body',parent.document).find('#aa').contents().find('body').html(); 
        alert(v); 
    }); 
    $('#write-parent-aa').click(function() 
    { 
        $('body',parent.document).find('#aa').contents().find('div').append('<hr> This is a bb.php operation aa.php Written content '); 
    }); 
}); 
</script> 
<div> 
 This is a iframe #bb The contents of  
</div> 
<input type="button" id="read-parent-aa" value=" Across the father read iframe #aa"> 
<input type="button" id="write-parent-aa" value=" Across the father to write iframe #aa">

HTML:

<!DOCTYPE html>
<meta charset="utf-8">
<title>jQuery operation iframe</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<iframe src="a.php" id="aa"></iframe>
<iframe src="b.php" id="bb"></iframe>
<input type="button" id="read-aa" value=" read iframe #aa">
<input type="button" id="write-aa" value=" write iframe #aa">
<script> 
$(function()
{
 $('#read-aa').click(function()
 {
  var v=$('#aa').contents().find('body').html();
  alert(v);
 });
 $('#write-aa').click(function()
 {

  $('#aa').contents().find('div').append('<hr> This is a index.php operation aa.php Written content ');
 });
});
</script>


Related articles: