我正在嘗試使用帶有 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 回答

catspeake
TA貢獻1111條經驗 獲得超0個贊
通過使用 CBroe 提到的 Readystate,我能夠獲得可靠的結果。
$('iframe').each(function(index, el) {
$(el).ready(function(){
el.style.height = el.contentWindow.document.body.scrollHeight + 'px';
});
});
- 1 回答
- 0 關注
- 163 瀏覽
添加回答
舉報
0/150
提交
取消