Teach you to use jquery to implement iframe adaptive height

  • 2020-03-30 03:19:02
  • OfStack

The iframe code, notice the ID


<iframe src="test.html" id="main" width="700" height="300" frameborder="0" scrolling="auto"></iframe>


Jquery code 1:


//Note: the following code is placed on the test.html call
$(window.parent.document).find("#main").load(function(){ 
var main = $(window.parent.document).find("#main"); 
var thisheight = $(document).height()+30; 
main.height(thisheight); 
});


Jquery code 2:


//Note: the following code is called on the same page as the iframe
$("#main").load(function(){ 
var mainheight = $(this).contents().find("body").height()+30; 
$(this).height(mainheight); 
}); 


Related articles: