有以下几种方法可以实现iframe自适应高度:
<script>
function resizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>
<iframe src="example.html" onload="resizeIframe(this)"></iframe>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
$('iframe').on('load', function() {
$(this).height($(this).contents().find('body').height());
});
});
</script>
<iframe src="example.html"></iframe>
height: auto
属性,使得iframe的高度自动适应其内容的高度。<style>
iframe {
height: auto;
}
</style>
<iframe src="example.html"></iframe>
这些方法可以根据具体的需求选择使用。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:iframe高度自适应的方法有哪些