我正在尝试使用具有 srcdoc 属性的 iframe 元素在页面上显示电子邮件。目前我正在使用此代码来调整 iframe 的大小。$(document).ready(function() { $('iframe').each(function(index, el) { el.onload = function() { el.style.height = el.contentWindow.document.body.scrollHeight + 'px'; } })});但它并不总是有效。有时内容会正确调整大小,有时则不会。我见过这样的解决方案https://medium.com/better-programming/how-to-automatically-resize-an-iframe-7be6bfbb1214,但据我所知,这需要我在 srcdoc 内容中注入一些 js 和然后监听 window.onmessage 事件。我觉得这是一个麻烦的解决方案。这是唯一的解决方案,还是有更好的方法来处理这个问题?
1 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
通过使用 CBroe 提到的 readystate,我能够获得可靠的结果。
$('iframe').each(function(index, el) {
$(el).ready(function(){
el.style.height = el.contentWindow.document.body.scrollHeight + 'px';
});
});
添加回答
举报
0/150
提交
取消
