javascript - src 为 base64 编码时如何获取 iframe 内容高度?
问题描述
这是我想动态显示内容的 iframe。
<iframe onload='resize()' id='ifr' src='data:text/html;charset=utf-8;base64,Encoded text here' width=100% height=400px></iframe>
我试过这样的东西..但高度总是返回 0
function resize() {
$('#ifr').height($('#ifr').contents().height());
}
这是jsfiddle ..
解决方案
$('iframe') 为您提供文档中的所有 iframe,因此您应该为框架提供 id 并像 $('#MYiframe') 一样使用它,或者从 jquert 返回的内容中获取第一个元素。
function resize() {
$('iframe')[0].height($('iframe')[0].contents().height());
}
而且我还在 SO 上找到了用于调整内容的 iframe 的大小。
iFrame.width = iFrame.contentWindow.document.body.scrollWidth;
iFrame.height = iFrame.contentWindow.document.body.scrollHeight;
推荐阅读
- react-native - React Native Text Input with Border and always Focused
- r - 基于列值的新类别列
- java - 错误:反向引用类型 (java.util.Set) 与托管类型不兼容
- postgresql - 使用 pg_restore 的 --jobs 参数没有提高性能
- javascript - jquery timepicker时间开始问题
- regex - 正则表达式忽略 Beyond Compare 中的小数位
- azure - 我能够使用遥测服务将日志从本地机器推送到 Azure 中的 AI。如何在 Azure 中将相同内容从 AI 推送到 OMS?
- csv - 使用 sed 获取带有标题的 tsv 文件
- git - 是否有充分的理由为什么应该在与 git repo 相同的目录层次结构级别创建工作树?
- clickhouse - 如何知道clickhouse中的查询使用了多少内存