我将在以后收到一些动态生成的iframe内容,并尝试创建一个无论高度如何都能很好地适应iframe的模板。因此,我想编写一个jquery脚本来读取iframe中的height属性的值,这样我就可以将父元素设置为相应的大小。
我不确定怎么做是访问iframe中的height属性...我假设最简单的做法是从父元素运行脚本来获取iframe高度,然后设置它自己的高度。我也希望iframe是相同的域,所以这不会是一个问题。
<div class="parent_element" style="">
<!-- .parent_element {height: } should be set to 400px -->
<iframe src="" height="400"></iframe>
</div有谁能给我指个方向吗?谢谢。
发布于 2012-04-24 06:34:15
$("#parent_element").height($("iframe").height());或者:
$("#parent_element").height($("iframe").attr("height"));发布于 2014-07-09 22:51:35
当iframe src加载时,调整iframe高度以匹配文档(加一点滚动条)。
$(document).ready( function () {
$('iframe').load( function() {
//console.log($(this).contents().height() + ' is the height');
$(this).height($(this).contents().height() + 25); // add some to the height to allow for horizontal scroll bars
});
});https://stackoverflow.com/questions/10289211
复制相似问题