<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>iframe</title> <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script> </head> <body> <iframe name="myiframe" id="myiframe" src="a.html" frameborder="0" scrolling="no"></iframe> <script type="text/javascript" language="javascript"> $(window.parent.document).find("#myiframe").load(function(){ var main = $(window.parent.document).find("#myiframe"); var thisheight = $(document).height()+30; main.height(thisheight); }); </script> </body> </html>
即使a.html页面的高度中间会变化,iframe也能自适应