我正在動態加載iFrame,有些頁面比其他頁面「更高」。我希望iFrame能夠相應增長。可能嗎?如果是這樣,怎麼樣?如何根據內容的大小「增長」iFrame?
4
A
回答
7
是的,這是可能的jQuery。 父頁代碼:
<iframe id='ifrm' />
腳本的iframe頁上:回發後
function alertSize() {
var myHeight = 0;
if (typeof (parent.window.innerWidth) == 'number') {
//Non-IE
myHeight = parent.window.innerHeight;
} else if (parent.document.documentElement
&& (parent.document.documentElement.clientWidth || parent.document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
myHeight = parent.document.documentElement.clientHeight;
} else if (parent.document.body && (parent.document.body.clientWidth || parent.document.body.clientHeight)) {
//IE 4 compatible
myHeight = parent.document.body.clientHeight;
}
//window.alert('Height = ' + myHeight);
return myHeight;
}
function AssignFrameHeight() {
var theFrame = $("#ifrm", parent.document.body);
var frameHeight1 = getIframeHeight('ifrm');
var frameHeight2 = $(document.body).height();
if ($(document.body)[0]) {
if ($(document.body)[0].bottomMargin)
frameHeight2 += Number($(document.body)[0].bottomMargin);
if ($(document.body)[0].topMargin)
frameHeight2 += Number($(document.body)[0].topMargin);
}
if (frameHeight1 > frameHeight2) {
theFrame.height(frameHeight1 - 20);
} else {
if ($.browser.msie)
theFrame.height(frameHeight2);
else
theFrame.height(frameHeight2 + 50);
}
}
function getIframeHeight(iframeName) {
//var iframeWin = window.frames[iframeName];
var iframeEl = parent.document.getElementById
? parent.document.getElementById(iframeName)
: parent.document.all
? parent.document.all[iframeName]
: null;
if (iframeEl) {
iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous
//var docHt = getDocHeight(iframeWin.document);
// need to add to height to be sure it will all show
var h = alertSize();
//var new_h = (h - 148);
//iframeEl.style.height = h + "px";
return h;
//alertSize();
}
}
重新分配高度:
function pageLoad() { // MS AJAX - UpdatePanel
AssignFrameHeight();
}
$(document).ready(function() { // jQuery
AssignFrameHeight();
});
2
你也許可以做到像
document.getElementById('your-iframe').height=new_height;
但如果你真的需要有一個iframe根據內容能夠生長,那麼我建議你嘗試另一個HTML元素作爲一個iframe可能不是你所需要的。
-2
嘗試使用寬度:100%;身高:100%並將它們應用於您的iframe元素
+1
它不會工作。你必須從字面上設置大小。 – 2010-05-20 11:36:28
+0
調整內部頁面大小後,這不起作用 – VMAtm 2010-05-20 11:41:31
相關問題
- 1. 如何根據iFrame大小調整頁面內容大小
- 2. 如何根據iframe的大小重新調整iframe的內容大小?
- 3. Android:EditText文字大小根據內容增長?可能的
- 4. 如何根據內容大小使iframe縮小和排?
- 5. 如何根據其內容調整iframe的大小?
- 6. Disqus的iframe如何根據其內容調整其大小?
- 7. 如何根據文本內容增加文本框的大小
- 8. iframe內容大小
- 9. iframe增長到指定的大小
- 10. 如何根據數據庫增長預測服務器大小?
- 11. 容器div高度不會根據內容增加大小
- 12. 如何根據內容大小自動調整NSTextView的大小?
- 13. 如何根據UINavigation bar的大小設置內容大小
- 14. 如何讓ListView增長到內容的大小
- 15. Firefox:根據實際內容調整iframe的大小
- 16. 根據內容高度自動調整iframe的大小
- 17. 根據iframe內容調整對話框的大小
- 18. 根據其內容動態調整iframe的大小
- 19. 如何根據表格大小根據其內容
- 20. 如何讓CommonsPoolTargetSource池大小根據需要增長?
- 21. 根據屏幕大小更改iframe內容
- 22. 根據iframe中的內容調整iframe及其父高度的大小
- 23. 根據內容長度修改NSTextField的字體大小
- 24. 如何通過iframe的是調整大小根據內容高度
- 25. 根據內容調整uiscrollview的大小
- 26. 根據內容調整UIView的大小
- 27. 根據內容調整textField的大小
- 28. 根據其內容的div大小
- 29. 根據內容的高度調整iFrame的大小隻能放大?
- 30. 如何根據內容動態縮小WebView大小?
不錯。加載瀏覽器黑客;) – naugtur 2010-05-20 12:06:25
適用於:IE 4-8,FF 3,Opera 10,Chrome 4.0,Safari(最新版本) – VMAtm 2010-05-20 12:15:26
拯救生命!謝謝 – bladefist 2011-11-12 18:40:17